Using filter_ in dplyr where both field and value are in variables

后端 未结 4 1441
离开以前
离开以前 2020-12-15 09:48

I want to filter a dataframe using a field which is defined in a variable, to select a value that is also in a variable. Say I have

df <- data.frame(V=c(6         


        
4条回答
  •  孤街浪徒
    2020-12-15 10:15

    Following on from LmW; personally I prefer using a dplyr pipeline where the dots are specified before the pipeline so that it is easier to use programmatically, say in a loop of filters.

    dots <-  paste0(fld," == '",sval,"'")
    df   %>% filter_(.dots = dots)
    

    LmW's example is correct but the values are hardcoded.

提交回复
热议问题