135

Problem

I would like to test if an element of a list exists, here is an example

foo <- list(a=1)
exists('foo') 
TRUE   #foo does exist
exists('foo$a') 
FALSE  #suggests that foo$a does not exist
foo$a
[1] 1  #but it does exist

In this example, I know that foo$a exists, but the test returns FALSE.

I looked in ?exists and have found that with(foo, exists('a') returns TRUE, but do not understand why exists('foo$a') returns FALSE.

Questions

  • Why does exists('foo$a') return FALSE?
  • Is use of with(...) the preferred approach?
3
  • 1
    maybe !is.null(foo$a) (or !is.null(foo[["a"]]) to be on the safe side) ? (or exists("a",where=foo))
    – Ben Bolker
    Commented Oct 10, 2011 at 23:27
  • 1
    @BenBolker thanks - would make a good answer; why is the latter option preferred? Commented Oct 10, 2011 at 23:29
  • 3
    @David partial matching... try the above with foo <- list(a1=1)
    – baptiste
    Commented Oct 11, 2011 at 4:40

7 Answers 7

181

This is actually a bit trickier than you'd think. Since a list can actually (with some effort) contain NULL elements, it might not be enough to check is.null(foo$a). A more stringent test might be to check that the name is actually defined in the list:

foo <- list(a=42, b=NULL)
foo

is.null(foo[["a"]]) # FALSE
is.null(foo[["b"]]) # TRUE, but the element "exists"...
is.null(foo[["c"]]) # TRUE

"a" %in% names(foo) # TRUE
"b" %in% names(foo) # TRUE
"c" %in% names(foo) # FALSE

...and foo[["a"]] is safer than foo$a, since the latter uses partial matching and thus might also match a longer name:

x <- list(abc=4)
x$a  # 4, since it partially matches abc
x[["a"]] # NULL, no match

[UPDATE] So, back to the question why exists('foo$a') doesn't work. The exists function only checks if a variable exists in an environment, not if parts of a object exist. The string "foo$a" is interpreted literary: Is there a variable called "foo$a"? ...and the answer is FALSE...

foo <- list(a=42, b=NULL) # variable "foo" with element "a"
"bar$a" <- 42   # A variable actually called "bar$a"...
ls() # will include "foo" and "bar$a" 
exists("foo$a") # FALSE 
exists("bar$a") # TRUE
4
  • 3
    it is still not clear - is there a reason why exists('foo$a') == FALSE? Commented Oct 11, 2011 at 2:45
  • This suggests there is generally no good solution for this in R! One might want more complex things (like testing if $mylist[[12]]$out$mcerror is defined) which would currently be complicated as hell.
    – Tomas
    Commented Sep 11, 2014 at 9:51
  • Were you aware of the where argument to exists pointed out in @Jim's answer? Commented Sep 16, 2014 at 22:10
  • "bar$a" <- 42 I really wish this was invalid syntax and exists("foo$a") worked in the naive sense.
    – Andy V
    Commented Oct 21, 2014 at 19:50
56

The best way to check for named elements is to use exist(), however the above answers are not using the function properly. You need to use the where argument to check for the variable within the list.

foo <- list(a=42, b=NULL)

exists('a', where=foo) #TRUE
exists('b', where=foo) #TRUE
exists('c', where=foo) #FALSE
1
  • 9
    Using exists() on a list does work, but I believe that R internally coerces it to an environment before checking for an object of that name, which is inefficient and can result in errors if there are any unnamed elements. For example if you run exists('a', list(a=1, 2)), it will give an error: Error in list2env(list(a = 1, 2), NULL, <environment>) : attempt to use zero-length variable name. The conversion happens here: github.com/wch/r-source/blob/…
    – wch
    Commented Dec 5, 2016 at 19:51
6

One solution that hasn't come up yet is using length, which successfully handles NULL. As far as I can tell, all values except NULL have a length greater than 0.

x <- list(4, -1, NULL, NA, Inf, -Inf, NaN, T, x = 0, y = "", z = c(1,2,3))
lapply(x, function(el) print(length(el)))
[1] 1
[1] 1
[1] 0
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 3

Thus we could make a simple function that works with both named and numbered indices:

element.exists <- function(var, element)
{
  tryCatch({
    if(length(var[[element]]) > -1)
      return(T)
  }, error = function(e) {
    return(F)
  })
}

If the element doesn't exist, it causes an out-of-bounds condition caught by the tryCatch block.

5

Here is a performance comparison of the proposed methods in other answers.

> foo <- sapply(letters, function(x){runif(5)}, simplify = FALSE)
> microbenchmark::microbenchmark('k' %in% names(foo), 
                                 is.null(foo[['k']]), 
                                 exists('k', where = foo))
Unit: nanoseconds
                     expr  min   lq    mean median   uq   max neval cld
      "k" %in% names(foo)  467  933 1064.31    934  934 10730   100  a 
      is.null(foo[["k"]])    0    0  168.50      1  467  3266   100  a 
 exists("k", where = foo) 6532 6998 7940.78   7232 7465 56917   100   b

If you are planing to use the list as a fast dictionary accessed many times, then the is.null approach might be the only viable option. I assume it is O(1), while the %in% approach is O(n)?

4

A slight modified version of @salient.salamander , if one wants to check on full path, this can be used.

Element_Exists_Check = function( full_index_path ){
  tryCatch({
    len_element = length(full_index_path)
    exists_indicator = ifelse(len_element > 0, T, F)
      return(exists_indicator)
  }, error = function(e) {
    return(F)
  })
}
0
4

rlang::has_name() can do this too:

foo = list(a = 1, bb = NULL)
rlang::has_name(foo, "a")  # TRUE
rlang::has_name(foo, "b")  # FALSE. No partial matching
rlang::has_name(foo, "bb")  # TRUE. Handles NULL correctly
rlang::has_name(foo, "c")  # FALSE

As you can see, it inherently handles all the cases that @Tommy showed how to handle using base R and works for lists with unnamed items. I would still recommend exists("bb", where = foo) as proposed in another answer for readability, but has_name is an alternative if you have unnamed items.

0

Use purrr::has_element to check against the value of a list element:

> x <- list(c(1, 2), c(3, 4))
> purrr::has_element(x, c(3, 4))
[1] TRUE
> purrr::has_element(x, c(3, 5))
[1] FALSE
2
  • Does it work if the element is nested / at any level of nesting? I checked the docs and it wasn't clear Commented Feb 3, 2019 at 21:53
  • @DavidLeBauer, no. In that case, I'd use rapply (something like any(rapply(x, function(v) identical(v, c(3, 4)), how = 'unlist'))) Commented Feb 4, 2019 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.