我有一个3列的data.frame,看起来像这样:
var1 var2 corr
1 OTU0001 OTU0004 0.882
2 OTU0001 OTU0014 0.656
3 OTU0004 OTU0014 0.456
4 OTU0001 OTU0015 0.690
5 OTU0004 OTU0015 0.612
6 OTU0014 OTU0015 0.565
7 OTU0001 OTU0016 0.652
8 OTU0004 OTU0016 0.630
9 OTU0014 OTU0016 0.465
10 OTU0015 OTU0016 0.884
11 OTU0001 OTU0017 0.766
例如,我想提取与出现“ OTU0016”匹配的行。我应该得到
7 OTU0001 OTU0016 0.652
8 OTU0004 OTU0016 0.630
9 OTU0014 OTU0016 0.465
10 OTU0015 OTU0016 0.884
我尝试了grep("^OTU0016$",Df)
,但只返回了integer(0)
答案 0 :(得分:2)
对于所有列,您都可以使用:
Df[rowSums(Df == "OTU0016") > 0,]
对于您的两列:
Df[rowSums(Df[c("var1", "var2") == "OTU0016") > 0,]
或
Df[Df$var1 == "OTU0016" | Df$var2 == "OTU0016",]
答案 1 :(得分:0)
使用dplyr
,您可以执行以下操作:
df %>%
filter_at(vars(starts_with("var")), any_vars(. == "OTU0016"))
var1 var2 corr
1 OTU0001 OTU0016 0.652
2 OTU0004 OTU0016 0.630
3 OTU0014 OTU0016 0.465
4 OTU0015 OTU0016 0.884
或使用base R
:
df[apply(df[, 1:2], 1, function(x) any(grepl("OTU0016", x, fixed = TRUE))), ]
var1 var2 corr
7 OTU0001 OTU0016 0.652
8 OTU0004 OTU0016 0.630
9 OTU0014 OTU0016 0.465
10 OTU0015 OTU0016 0.884
或者:
df[apply(df[, -grepl("corr", names(df), fixed = TRUE)], 1, function(x) any(grepl("OTU0016", x, fixed = TRUE))), ]
或者:
df[apply(df[, -grepl("corr", names(df), fixed = TRUE)], 1, function(x) any(match(x, "OTU0016", nomatch = 0))), ]