library(dplyr)
mydata <- data.frame(id = c(1, 2, 3, 4, 5),
fruit = c("orange", "jackfruit", "", "N/A", ""),
fruit2 = c("orange", "guava", "", "", ""),
fruit3 = c("orange", "N/A", "orang", "", ""))
> mydata
id fruit fruit2 fruit3
1 1 orange orange orange
2 2 jackfruit guava N/A
3 3 orange
4 4 N/A
5 5
我有一个数据集,我想检查每个ID是否存在字符串。例如,字符串“橙子”存在于ids = 1, 2
中,字符串“jackfruit”存在于id=2
中,等等。
下面是我的尝试,但出现错误:
> mydata %>% group_by(id) %>% grepl("orange")
Warning message:
In grep(., "orange") :
argument 'pattern' has length > 1 and only the first element will be used
1条答案
按热度按时间vawmfj5a1#
我不知道你是如何期待作为输出,但我想到了两种方法来做到这一点。
通用
代码
输出
手动
代码
输出