如何使用grep或任何其他库根据具有“car”但不包含“carry”的列值过滤行?
rows <- (df[grep("car", df$item_text),])
答案 0 :(得分:0)
rows <- df[df$item_text=="car",]
答案 1 :(得分:0)
假设你确实想要'car1'和'car2',而不是'carry':
rows <- df[grepl("car", df$item_text) & !grepl("carry", df$item_text),]
示例数据:
df = data.frame(item_text=c("car1","car2","carry","carry1"),id=seq(1,4))
示例输出:
item_text id
1 car1 1
2 car2 2
答案 2 :(得分:0)
df = data.frame(item_text = c("car", "a car", "my carry", "carry a car", "something"))
subset(df, subset = grepl("car", df$item_text) & !grepl("carry", df$item_text))