0

所以,我想使用 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”等的行(删除)。可以在一个函数中完成还是我必须像我一样编写 3 个不同的代码?

4

1 回答 1

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")), ]
于 2013-10-29T12:45:15.000 回答