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

后端 未结 4 1428
离开以前
离开以前 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

    Here's an alternative with base R, which is maybe not very elegant, but it might have the benefit of being rather easily understandable:

    df[df[colnames(df)==fld]==sval,]
    #  V Unhappy
    #2 1       Y
    #3 5       Y
    #4 3       Y
    

提交回复
热议问题