如何获得仅在两列中具有相同条目的特定观察

时间:2017-05-16 07:49:39

标签: r

v1=rep(c("a","b","c","d"),each=5)
v2=rep(c(10,12,9,7),each=5)
v3=c("y","y","w","y","y",rep(c("f"),times=5),rep(c("y"),times=4),"w",rep(c("k"),times=5))
v4=c("j","j","w","j","j",rep(c("q"),times=5),rep(c("l"),times=4),"w",rep(c("n"),times=5))
df=data.frame(v1,v2,v3,v4)

   v1 v2 v3 v4
1   a 10  y  j
2   a 10  y  j
3   a 10  w  w
4   a 10  y  j
5   a 10  y  j
6   b 12  f  q
7   b 12  f  q
8   b 12  f  q
9   b 12  f  q
10  b 12  f  q
11  c  9  y  l
12  c  9  y  l
13  c  9  y  l
14  c  9  y  l
15  c  9  w  w
16  d  7  k  n
17  d  7  k  n
18  d  7  k  n
19  d  7  k  n
20  d  7  k  n
> 

我希望继续关注v3="w"v4="w"

的观察结果
   v1 v2 v3 v4
1   a 10  y  j
2   a 10  y  j
3   a 10  w  w
4   a 10  y  j
5   a 10  y  j
6   c  9  y  l
7   c  9  y  l
8   c  9  y  l
9   c  9  y  l
10  c  9  w  w

我该怎么做呢

4 个答案:

答案 0 :(得分:5)

请使用空格。除此之外的任何编码风格都会更好。关于你的问题,你需要做一些@agerom和@thotal建议的事情,并加以扭曲。从子集中收集这些v1值,并使用它们进一步对data.frame进行子集化。

> df[df$v1 %in% unique(df[df$v4 == "w" & df$v3 == "w", ]$v1), ]
   v1 v2 v3 v4
1   a 10  y  j
2   a 10  y  j
3   a 10  w  w
4   a 10  y  j
5   a 10  y  j
11  c  9  y  l
12  c  9  y  l
13  c  9  y  l
14  c  9  y  l
15  c  9  w  w

答案 1 :(得分:1)

您可以选择以下行:

df[df$v3 == "w" & df$v4 == "w", ]
#    v1 v2 v3 v4
# 3   a 10  w  w
# 15  c  9  w  w    

答案 2 :(得分:0)

以下是data.table

的解决方案
library("data.table")
dt <- data.table(v1=rep(c("a","b","c","d"), each=5),
                 v2=rep(c(10,12,9,7), each=5),
                 v3=rep(c("y","w","y", "f", "y", "w", "k"), times=c(2,1,2, 5,4,1,5)),
                 v4=rep(c("j","w","j", "q", "l", "w", "n"), times=c(2,1,2, 5,4,1,5))
)
dt[, sel:=any(v3=='w' & v4=='w'), v1][sel==TRUE]
#    v1 v2 v3 v4  sel
# 1:  a 10  y  j TRUE
# 2:  a 10  y  j TRUE
# 3:  a 10  w  w TRUE
# 4:  a 10  y  j TRUE
# 5:  a 10  y  j TRUE
# 6:  c  9  y  l TRUE
# 7:  c  9  y  l TRUE
# 8:  c  9  y  l TRUE
# 9:  c  9  y  l TRUE
# 10:  c  9  w  w TRUE

dt[, sel:=any(v3=='w' & v4=='w'), v1][(sel), .(v1,v2,v3,v4)]

答案 3 :(得分:-1)

使用以下表达式:

df[df$v3=="w" & df$v4=="w",]