简体   繁体   中英

R if error for subscript out of bounds

I'm looping through a nested loop but not sure how to deal with when one index doesn't have any data.

For example:

A <- list(list(1,2), list(2), list(4,5))

I can do:

for(i in 1:length(A)){print(A[[i]][[1]])}
[1] 1
[1] 2
[1] 4

However the following fails:

for(i in 1:length(A)){print(A[[i]][[2]])}
[1] 2
Error in A[[i]][[2]] : subscript out of bounds

I cannot simply wrap it in a if(is.null(...)){"Null"}else{...} as I had done previously.

Is there any generic if error or is error function that would give a value whenever I get the subscript out of bounds error? This might not be needed for the above example, but my data is a little bit more complicated and I dont know how to replicate it but will show what I was trying to loop through, what it looks like when empty and when there is a result:

for(i in 1:x$count){z=rbind.fill(z,data.frame(data=if(is.null(x$results[[i]]$experiences[[1]]$experience$A)){"Null"}else{x$results[[i]]$experiences[[1]]$experience$A}

Empty:

x$results[[4]]$experiences
list()

result:

x$results[[3]]$experiences
[[1]]
[[1]]$experience
[[1]]$experience$start
[1] "..."

[[1]]$experience$A
[1] "..."

[[1]]$experience$B
[[1]]$experience$B$A
[1] "..."


[[1]]$experience$C
[[1]]$experience$C$A
NULL

[[1]]$experience$C$B
NULL

[[1]]$experience$C$C
NULL



[[1]]$`_meta`
[[1]]$`_meta`$weight
[1] 1

[[1]]$`_meta`$`_sources`
[[1]]$`_meta`$`_sources`[[1]]
[[1]]$`_meta`$`_sources`[[1]]$`_origin`
[1] "..."





[[2]]
[[2]]$experience
[[2]]$experience$start
[1] "..."

[[2]]$experience$A
[1] "..."

[[2]]$experience$B
[[2]]$experience$B$C
[1] "..."


[[2]]$experience$C
[[2]]$experience$C$A
NULL

[[2]]$experience$C$B
NULL

[[2]]$experience$C$C
NULL



[[2]]$`_meta`
[[2]]$`_meta`$weight
[1] 1

[[2]]$`_meta`$`_sources`
[[2]]$`_meta`$`_sources`[[1]]
[[2]]$`_meta`$`_sources`[[1]]$`_origin`
[1] "..."





[[3]]
[[3]]$experience
[[3]]$experience$start
[1] "..."

[[3]]$experience$A
[1] "..."

[[3]]$experience$B
[[3]]$experience$B$A
[1] "..."


[[3]]$experience$C
[[3]]$experience$C$A
NULL

[[3]]$experience$C$B
NULL

[[3]]$experience$C$C
NULL



[[3]]$`_meta`
[[3]]$`_meta`$weight
[1] 1

[[3]]$`_meta`$`_sources`
[[3]]$`_meta`$`_sources`[[1]]
[[3]]$`_meta`$`_sources`[[1]]$`_origin`
[1] "..."

Typically, you'd use tryCatch for error handling. An example, where NA is printed if subsetting results in an error:

for(i in seq_along(A)){
  tryCatch(print(A[[i]][[2]]),
           error = function(e) print(NA))
}
#[1] 2
#[1] NA
#[1] 5

PS: Growing an object in a loop as you appear to do in your real code is a cardinal performance sin. Learn to preallocate.

PPS: Instead of handling the error, it might be better to avoid it, eg, by checking the lengths: A[lengths(A) > 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM