在 dplyr 中使用 filter_,其中字段和值都在变量中 [英] Using filter_ in dplyr where both field and value are in variables

查看:17
本文介绍了在 dplyr 中使用 filter_,其中字段和值都在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在变量中定义的字段过滤数据框,以选择也在变量中的值.说我有

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, 1, 5, 3, 2), Unhappy=c("N", "Y", "Y", "Y", "N"))
fld <- "Unhappy"
sval <- "Y"

我想要的值是 df[df$Unhappy == "Y", ].

我已经阅读了 nse 小插图以尝试使用 filter_ 但不太理解它.我试过了

I've read the nse vignette to try use filter_ but can't quite understand it. I tried

df %>% filter_(.dots = ~ fld == sval)

没有返回任何内容.我得到了我想要的

which returned nothing. I got what I wanted with

df %>% filter_(.dots = ~ Unhappy == sval)

但显然这违背了使用变量来存储字段名称的目的.请问有什么线索吗?最终我想使用它,其中 fld 是字段名称向量,svalfld 中每个字段的过滤器值向量.

but obviously that defeats the purpose of having a variable to store the field name. Any clues please? Eventually I want to use this where fld is a vector of field names and sval is a vector of filter values for each field in fld.

推荐答案

你可以试试用 interp from lazyeval

You can try with interp from lazyeval

 library(lazyeval)
 library(dplyr)
 df %>%
     filter_(interp(~v==sval, v=as.name(fld)))
 #   V Unhappy
 #1 1       Y
 #2 5       Y
 #3 3       Y

对于多个键/值对,我发现这可行,但我认为应该有更好的方法.

For multiple key/value pairs, I found this to be working but I think a better way should be there.

  df1 %>% 
    filter_(interp(~v==sval1[1] & y ==sval1[2], 
           .values=list(v=as.name(fld1[1]), y= as.name(fld1[2]))))
 #  V Unhappy Col2
 #1 1       Y    B
 #2 5       Y    B

对于这些情况,我发现 base R 选项更容易.例如,如果我们试图根据 'fld1' 中的 'key' 变量和 'sval1' 中的相应值来filter 行,一个选项是使用 Map.我们对数据集 (df1[fld1]) 进行子集,并将 FUN (==) 应用到具有相应值的 df1[f1d1] 的每一列在 'sval1' 中,并使用 &Reduce 来获得可用于 filter 'df1' 行的逻辑向量.

For these cases, I find the base R option to be easier. For example, if we are trying to filter the rows based on the 'key' variables in 'fld1' with corresponding values in 'sval1', one option is using Map. We subset the dataset (df1[fld1]) and apply the FUN (==) to each column of df1[f1d1] with corresponding value in 'sval1' and use the & with Reduce to get a logical vector that can be used to filter the rows of 'df1'.

 df1[Reduce(`&`, Map(`==`, df1[fld1],sval1)),]
 #   V Unhappy Col2
 # 2 1       Y    B
  #3 5       Y    B

数据

df1 <- cbind(df, Col2= c("A", "B", "B", "C", "A"))
fld1 <- c(fld, 'Col2')
sval1 <- c(sval, 'B')    

这篇关于在 dplyr 中使用 filter_,其中字段和值都在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆