rlistnullabbreviation

Is there a way to disable R abbreviation in list indexing?


at the moment I'm quite stunned by this piece of code:

bad.list <- list(xx = "A")
print(is.null(bad.list$x))   # FALSE: Bad since there is no x
print(is.null(bad.list$xx))  # FALSE: Correct since there is xx
print(is.null(bad.list$xxx)) # TRUE:  Correct since there is no xxx

Normally you would expect is.null(bad.list$x) to be evaluated as TRUE since there is no x in the list. But since R allows you to use abbreviations it evaluates to FALSE. In my situation I can't change the names of the list entries.

Is there a way to force R to disable abbreviations?


Solution

  • You can use the [[...]] notation:

    bad.list <- list(xx = "A")
    print(is.null(bad.list[["x"]]))   # is TRUE now
    print(is.null(bad.list[["xx"]]))  # FALSE: Correct since there is xx
    print(is.null(bad.list[["xxx"]])) # TRUE:  Correct since there is no xxx