所以,我想使用grep函数选择一些行,但它对我不起作用:
这就是我使用的代码:
set.seed(2)
tbl_bio <- tbl_reo[grepl("Biotin", tbl_reo$modifications), , drop = FALSE] ## selecting rows from column "modifications" with string Biotin - that works good
tbl_bio1 <- tbl_bio[grep1("BiotinControl1_2", tbl_bio$variable), , drop = TRUE]
tbl_bio2 <- tbl_bio1[grepl("BiotinControl2", tbl_bio1$variable), , drop = TRUE]
tbl_bio3 <- tbl_bio2[grepl("BiotinControl3", tbl_bio2$variable), , drop = TRUE]
问题在于其他三行代码,因为我将drop设置为TRUE所以我认为我只删除包含我提到的字符串的行。 运行此类代码后,我没有任何数据。如何删除包含“BiotinControl1_2”等的行(drop)。 它可以在一个函数中完成,还是像我一样编写3个不同的代码?
答案 0 :(得分:0)
这是使用一些虚假数据的示例。希望它有所帮助。
df <- structure(list(mod = c("b", "d", "b", "e", "b", "b", "b", "b",
"b", "b", "b", "f", "d", "f", "d", "f", "f", "c", "a", "b"),
var = c("K", "L", "H", "G", "J", "I", "G", "K", "H", "G",
"L", "G", "K", "K", "J", "L", "I", "H", "H", "H"), x = c(47L,
18L, 24L, 91L, 27L, 23L, 97L, 19L, 73L, 20L, 90L, 25L, 92L,
98L, 37L, 62L, 54L, 52L, 49L, 56L)), .Names = c("mod", "var",
"x"), row.names = c(NA, -20L), class = "data.frame")
# select all rows with variable mod equal to string "b"
df[df$mod=="b", ]
# select all rows with variable var NOT equal to string "I", "J", "L"
df[!(df$var %in% c("I", "J", "L")), ]
# select all rows with variable mod equal to string "b" AND variable var NOT equal to string "I", "J", "L"
df[df$mod=="b" & !(df$var %in% c("I", "J", "L")), ]