2

I have a dataset called df, which has columns a and b with three integers each. I want to write a function for the mean (obviously this already exists; I want to write a larger function and this appears to be where problems are occurring). However, this function returns NA:

mean_function <- function(x) {
mean(df$x)
}

mean_function(a) returns NA, while mean(df$a) returns 2. Is there something I'm missing about how R functions handle datasets, or another problem?

eclare
  • 139
  • 5

1 Answers1

2

We need [[ instead of $ as it will literally check for x as column and pass a string

mean_function <- function(x) {mean(df[[x]])}
mean_function("a")

If we need to pass unquoted column name, substitute and convert to character with deparse

mean_function<- function(x) {
       x <- deparse(substitute(x))
       mean(df[[x]]
 }

mean_function(a)
akrun
  • 874,273
  • 37
  • 540
  • 662