A column in a data.frame can be accessed using $
, like so:
iris$Sepal.Length
# [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1
# [19] 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0
The data.frame column can also be accessed with $
using backtick syntax:
`$`(iris, "Sepal.Length")
# [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1
# [19] 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0
However, if a variable or function returning the string (in this case "Sepal.Length"
) is provided as the second argument, the results are different.
Examples
One might expect the same data.frame column to be returned when it is specified via a variable:
var = "Sepal.Length"
`$`(iris, var)
# NULL
or via a function:
`$`(iris, ifelse(TRUE, "Sepal.Length", NA))
Error in `$`(iris, ifelse(TRUE, "Sepal.Length", NA)) :
invalid subscript type 'language'
Question
Why does $
seemingly behave differently when its second argument is provided by a variable or a function, as opposed to a string?