1
fun <- function(dataframe, var){
    print(data$var)
}

dataset <- data.frame(a = 1:6, b = 12:17, c = 3:8)

fun(dataset, a)

Essentially, I would like to do something like this where I can just pass one of the column names into a function and be able to use it to call that column later on.

I know the group_by function handles it somehow, but when I went into debugging mode and tried to figure it out, I couldn't make heads or tails of what was actually doing what I wanted.

Kyle
  • 61
  • 8

1 Answers1

-1

To do this dplyr-like, you need to use tidyevaluation. First, you need to enquo() var, and then refer to var with !! :

library(dplyr)

fun <- function(dataframe, var){
  var <- enquo(var)
  select(dataframe, !!var)
}

dataset <- data.frame(a = 1:6, b = 12:17, c = 3:8)

fun(dataset, a)

You can find a good tidyeval tuto here.

Best,

Colin

Colin FAY
  • 4,849
  • 1
  • 12
  • 29