我有如下所示的数据:
A B
1 unicorn in the field
2 dog house in the yard
3 frog in the lake
4 house in the city如果b有单词"House“或"Dog House”,我正在尝试使用这些数据创建一个新的数据框。我试过了
dogdata<-which(df$B == grepl('house|dog house',df$B,ignore.case = TRUE)),A所以我希望我的结果是
A B
2 dog house in the yard
4 house in the city但我一直收到错误。谢谢
发布于 2018-08-09 02:52:19
你的子集符号有点离谱,实际上你根本不需要which:
df[grepl('house|dog house', df$B, ignore.case = TRUE),]grepl返回一个TRUE/FALSE向量,我们可以用它来子集。
另外,为了反转您的选择(即选择不包含"house“或"dog house”的行),您必须使用!而不是-:
df[!grepl('house|dog house', df$B, ignore.case = TRUE),]https://stackoverflow.com/questions/51753558
复制相似问题