【发布时间】:2020-02-25 22:46:59
【问题描述】:
我认为没有人问过这个确切的问题 - 很多关于基于一个值(即x[grepl("some string", x[["column1"]]),])的子集的东西,但不是多个值/字符串。
这是我的数据示例:
#create sample data frame
data = data.frame(id = c(1,2,3,4), phrase = c("dog, frog, cat, moose", "horse, bunny, mouse", "armadillo, cat, bird,", "monkey, chimp, cow"))
#convert the `phrase` column to character string (the dataset I'm working on requires this)
data$phrase = data$phrase
#list of strings to remove rows by
remove_if = c("dog", "cat")
这将给出一个如下所示的数据集:
id phrase
1 1 dog, frog, cat, moose
2 2 horse, bunny, mouse
3 3 armadillo, cat, bird,
4 4 monkey, chimp, cow
我想删除第 1 行和第 3 行(因为第 1 行包含“狗”,第 3 行包含“猫”),但保留第 2 行和第 4 行。
id phrase
1 2 horse, bunny, mouse
2 4 monkey, chimp, cow
换句话说,我想对data 进行子集化,使其只有(标题和)第 2 行和第 4 行(因为它们既不包含“狗”也不包含“猫”)。
谢谢!
【问题讨论】: