1

我有 data.frame :

df1<-data.frame("Sp1"=1:6,"Sp2"=7:12,"Sp3"=13:18)
rownames(df1)=c("A","B","C","D","E","F")

df1

  Sp1 Sp2 Sp3
A   1   7  13
B   2   8  14
C   3   9  15
D   4  10  16
E   5  11  17
F   6  12  18

df1我按截止值过滤rowSums(df1)并返回要包括在下游分析中的站点(行名称)。

include<-rownames(df1[rowSums(df1)>=22,])
include

[1] "B" "C" "D" "E" "F"

我有第二个 data.frame :

df2<-data.frame(site.x=c("A","B","C"), site.y=c("D","E","F"),score=1:3)

  site.x site.y score
1      A      D     1
2      B      E     2
3      C      F     3

我想过滤 df2 使其仅包含与“包含”中列出的站点完全相同的行,df2$site.xdf2$site.y过滤掉包含“A”的行并返回。

  site.x site.y score    
2      B      E     2
3      C      F     3

我试过了 :

filter<-df2$site.x == include & df2$site.y == include   

filtered<-df2[filter,]

感谢您的任何建议!

回答

用于%

filter<-df2$site.x %in% include & df2$site.y =%in% include  

filtered<-df2[filter,]
filtered

   site.x site.y score
 2      B      E     2
 3      C      F     3
4

1 回答 1

1

对我来说,它适用于:

filter<-df2$site.x %in% include & df2$site.y %in% include
df2[filter,]

事实上,你已经把df1而不是放在df2你问题的最后两行。

于 2012-07-24T12:34:35.800 回答