Dollar operator as function argument for sapply not working as expected

后端 未结 2 1629
遥遥无期
遥遥无期 2021-02-13 16:17

I have the following list

test_list=list(list(a=1,b=2),list(a=3,b=4))

and I want to extract all elements with list element name a.

2条回答
  •  再見小時候
    2021-02-13 16:41

    evaluation vs. none

    [[ evaluates its argument whereas $ does not. L[[a]] gets the component of L whose name is held in the variable a. $ just passes the argument name itself as a character string so L$a finds the "a" component of L. a is not regarded as a variable holding the component name -- just a character string.

    Below L[[b]] returns the component of L named "a" because the variable b has the value "a" whereas L$b returns the componet of L named "b" because with that syntax b is not regarded as a variable but is regarded as a character string which itself is passed.

    L <- list(a = 1, b = 2)
    b <- "a"
    L[[b]] # same as L[["a"]] since b holds a
    ## [1] 1
    L$b  # same as L[["b"]] since b is regarded as a character string to be passed
    ## [1] 2
    

    sapply

    Now that we understand the key difference bewteen $ and [[ to see what is going on with sapply consider this example. We have made each element of test_list into a "foo" object and defined our own $.foo and [[.foo methods which simply show what R is passing to the method via the name argument:

    foo_list <- test_list
    class(foo_list[[1]]) <- class(foo_list[[2]]) <- "foo"
    
    "$.foo" <- "[[.foo" <- function(x, name) print(name)
    
    result <- sapply(foo_list, "$", "a")
    ## "..."
    ## "..."
    
    result2 <- sapply(foo_list, "[[", "a")    
    ## [1] "a"
    ## [1] "a"
    

    What is happening in the first case is that sapply is calling whatever$... and ... is not evaluated so it would be looking for a list component which is literally named "..." and, of course, there is no such component so whatever$... is NULL hence the NULLs shown in the output in the question. In the second case whatever[[[...]] evaluates to whatever[["a"]] hence the observed result.

提交回复
热议问题