Looking at this question it is possible to test if an element of a list exists, provided your list has names
:
foo <- list(a=1)
"a" %in% names(list) # TRUE
"b" %in% names(list) # FALSE
However it is not clear if or how this can be extended to a list which isn't named:
foo <- list(1)
names(list) # NULL
I can test this using tryCatch
but it isn't particularly elegant:
indexExists <- function(list, index) {
tryCatch({
list[[index]] # Efficiency if element does exist and is large??
TRUE
}, error = function(e) {
FALSE
})
}
Is there a better way of going about this?