根据数据框中是否存在字符串过滤行
Filter rows by presence of string in dataframe
此问题与 Delete rows containing specific strings in R and R, remove row if there is a certain character 相似,但不重复。问题是如果字符串出现在数据框中行中的任何位置,如何删除该行,而不必在函数中写入列的名称。
编辑:在答案中删除 !
如果 objective 是保留这些行。
你可以这样做。
删除所有包含 "bee"
的行
df <- data.frame(c("a", "c", "d", "h"),c("bee","f","g","i"))
df<-df[apply(df,1,function(rowdata){
!any(grepl("bee", rowdata))
}),]
正如@Ferroao 上面指出的那样,为了仅保留包含 "bee" 的行,请删除 !
(在本例中代表 'not')。
如果您希望保留每个值都包含 "bee" 的行,您可以使用 all()
而不是 any()
。
此问题与 Delete rows containing specific strings in R and R, remove row if there is a certain character 相似,但不重复。问题是如果字符串出现在数据框中行中的任何位置,如何删除该行,而不必在函数中写入列的名称。
编辑:在答案中删除 !
如果 objective 是保留这些行。
你可以这样做。
删除所有包含 "bee"
的行df <- data.frame(c("a", "c", "d", "h"),c("bee","f","g","i"))
df<-df[apply(df,1,function(rowdata){
!any(grepl("bee", rowdata))
}),]
正如@Ferroao 上面指出的那样,为了仅保留包含 "bee" 的行,请删除 !
(在本例中代表 'not')。
如果您希望保留每个值都包含 "bee" 的行,您可以使用 all()
而不是 any()
。