Pass a string as variable name in dplyr::filter

2020-01-24 21:12发布

问题:

I'm using mtcars dataset to illustrate my question.

For example, I want to subset data to 4-cyl cars.I can do:

mtcars %>% filter(cyl == 4)

In my work, I need to pass a string variable as my column name. For example:

var <- 'cyl'
mtcars %>% filter(var == 4)

I also did:

mtcars %>% filter(!!var == 4)

In both cases, I got empty dataframe.

回答1:

!! or UQ evaluates the variable, so mtcars %>% filter(!!var == 4) is the same as mtcars %>% filter('cyl' == 4) where the condition always evaluates to false; You can prove this by printing !!var in the filter function:

mtcars %>% filter({ print(!!var); (!!var) == 4 })
# [1] "cyl"
#  [1] mpg  cyl  disp hp   drat wt   qsec vs   am   gear carb
# <0 rows> (or 0-length row.names)

To evaluate var to the cyl column, you need to convert var to a symbol of cyl first, then evaluate the symbol cyl to a column:

Using rlang:

library(rlang)
var <- 'cyl'
mtcars %>% filter((!!sym(var)) == 4)

#    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#1  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#2  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#3  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
# ...

Or use as.symbol/as.name from baseR:

mtcars %>% filter((!!as.symbol(var)) == 4)

mtcars %>% filter((!!as.name(var)) == 4)


回答2:

I think @snoram's answer is elegant and is dependent solely on dplyr.

var <- c('cyl')
mtcars %>% filter(get(var) == 4)

You can also use this with a list. For a simple example, you can get a count of each filtered column as a new dataset.

#adding car name
mtcars <- rownames_to_column(mtcars, "car_name")

#name your vectors
vector <- c("vs","am","carb")

df2 <- data.frame()
for (variable in vector) {
  df1 <- mtcars %>% filter(get(variable) == 1) %>% summarise(variable = n_distinct(car_name)) %>% data.frame()

  df2<- rbind(df2,df1)
}


回答3:

You can use eval(parse(text= to evaluate strings as variables:

mtcars %>% filter(eval(parse(text='cyl')) == 4)



标签: r dplyr