How to use a variable in dplyr::filter?

后端 未结 3 390
悲&欢浪女
悲&欢浪女 2020-12-05 10:29

I have a variable with the same name as a column in a dataframe:

df <- data.frame(a=c(1,2,3), b=c(4,5,6))
b <- 5

I want to get the ro

3条回答
  •  爱一瞬间的悲伤
    2020-12-05 11:03

    You could use the get function to fetch the value of the variable from the environment.

    df %>% filter(b == get("b")) # Note the "" around b
    

提交回复
热议问题