我有一个数据框,上面有动物的名字
x=data.frame(c("lion","tiger","cow","vulture","hyena","leopard","deer","gazzelle"))
colnames(x)=c('animal')
#create sets of animals based on the their diet
carnivore=c("lion","tiger","leopard")
herbivore=c("cow","deer","gazzelle")
omnivore=c("vulture","hyena")
x$diet=NA ## Add a new column where I want to enter if the animal is one of herbivore, carnivore or omnivore based on the value in x$animal
for (i in 1:(nrow(x))) {
if (x$animal[i] 'is a subset of' "Omnivore") {
x$diet[i] = "Omnivore"
} else if (x$animal[i] 'is a subset of' "Carnivore") {
x$diet[i] = "Carnivore"
} else if (x$animal[i] 'is a subset of' "Herbivore") {
x$diet[i] = "Herbivore"
}
}
我不太能在R中编写等同于‘is a subset’的代码,我相信它肯定有一个简单的解决方案.如有任何帮助,不胜感激