在dplyr中使用filter_,其中field和value都在变量中 [英] Using filter_ in dplyr where both field and value are in variables
问题描述
df< - data.frame(V = c(6,1,5,3,2) Unhappy = c(N,Y,Y,Y,N))
fld < - 不快乐
sval < - Y
我想要的值为 df [df $ Unhappy ==Y,]
。
我已阅读 nse
小插曲尝试使用 filter _
但不太明白。我试过
df%>%filter _(。dots =〜fld == sval)
什么都没有返回。我得到了我想要的
df%>%filter _(。dots =〜Unhappy == sval)
但显然,失败的目的是让变量存储字段名称。有什么线索吗?最终我想使用这个,其中 fld
是字段名称的向量,而$ code> sval 是每个过滤器值的向量
您可以尝试使用 interp
从 lazyeval
库(lazyeval)
库(dplyr)
df%>%
filter_(interp(〜v == sval,v = as.name(fld)))
#V不快乐
#1 1 Y
#2 5 Y
#3 3 Y
对于多个键/值对,我发现这是工作,但我认为应该有更好的方法。
df1%>%
filter_(interp(〜v == sval1 [1]& y == sval1 [2],
.values = list(v = as.name(fld1 [1 ]),y = as.name(fld1 [2]))))
#V不快乐Col2
#1 1 YB
#2 5 YB
对于这些情况,我发现 base R
选项更容易。例如,如果我们试图根据'fld1'中的'key'变量('sval1'中的相应值)过滤器,那么一个选项就是使用地图
。我们将数据集( df1 [fld1]
)进行子集,并将FUN( ==
)应用于 df1 [f1d1]
在sval1中具有相应的值,并将&
与 code>获得可用于
过滤器
df1行的逻辑向量。
df1 [Reduce(`&`,Map(`==`,df1 [fld1],sval1))]]
#V不快乐Col2
#2 1 YB
#3 5 YB
数据
df1 < - cbind(df,Col2 = c(A,B,B,C,A))
fld1< - c(fld,'col2')
sval1 < - c(sval,'B')
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"
The value I want would be df[df$Unhappy == "Y", ]
.
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)
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
.
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
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
data
df1 <- cbind(df, Col2= c("A", "B", "B", "C", "A"))
fld1 <- c(fld, 'Col2')
sval1 <- c(sval, 'B')
这篇关于在dplyr中使用filter_,其中field和value都在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!