[英]Subsetting in R with multiple criteria
I have a dataset as follows (in data.frame format): 我有一个数据集如下(data.frame格式):
>dataset
X Y Z Value
a c f 12
a d f 45
a c f 654
a d g 684
a c g 54
b d f 78
b c f 31
b d f 777
b c g 54
b d g 45
And I have an second data.frame with criteria: 我有第二个data.frame标准:
>criteria
X Y Z
a c f
b d f
How do I apply the second matrix to the first to get, in this example, c(654, 12, 777, 68) as a result? 在这个例子中,如何将第二个矩阵应用于第一个矩阵,结果是c(654,12,777,68)? Most of the things I've tried end up pulling out all the lines with any of the three variables matching instead of all three.
我尝试过的大多数事情最终会将三个变量中的任何一个匹配而不是全部三个。
EDIT: Fixed what the result is supposed to be 编辑:修正了结果应该是什么
Just use merge
: 只需使用
merge
:
merge(df1, df2)
If you want just the vector: 如果你只想要矢量:
merge(df1, df2)[,'Value']
Data: 数据:
df1 <- read.table(text =
'X Y Z Value
a c f 12
a d f 45
a c f 654
a d g 684
a c g 54
b d f 78
b c f 31
b d f 777
b c g 54
b d g 45', h = T)
df2 <- read.table(text = '
X Y Z
a c f
b d f', h = T)
adding some points on y logic : 在y逻辑上添加一些点:
do.call(paste0, criteria)
# [1] "acf" "bdf"
do.call(paste0, dataset[1:3])
# [1] "acf" "adf" "acf" "adg" "acg" "bdf" "bcf" "bdf" "bcg" "bdg"
v = do.call(paste0, dataset[1:3]) %in% do.call(paste0, criteria)
# [1] TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE
Now this logical vector is used to subset the Value column 现在,此逻辑向量用于对“值”列进行子集化
dataset$Value[v]
# [1] 12 654 78 777
We can use the tidyverse
我们可以使用
tidyverse
library(tidyverse)
inner_join(df1, df2) %>%
select(Value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.