I'm writing a function where I'd like to be able to pass in variables from a data frame as atomic vectors, like df$var
(e.g., mtcars$mpg
).
To keep the example very simple, say the function just returns data.frame(table(df$var))
:
foo.function <- function(var) {
data.frame(table(var))
}
head(foo.function(mtcars$mpg))
#> var Freq
#> 1 10.4 2
#> 2 13.3 1
#> 3 14.3 1
#> 4 14.7 1
#> 5 15 1
#> 6 15.2 2
Notice that the name of the tabulated variable in the returned table is the internal name of the passed object (var
) rather than it's "original" name, which was mpg
. Is it possible to retrieve mpg
(just the name) from within the function (without changing or adding arguments)? I was inclined to say no, since R is just receiving a vector of values, but I suspect R may have this capacity based on what it can do with NSE.