使用“IF”运算符检查数据帧单元格中的给定值是否属于 R 中值的“集合”

问题描述 投票:0回答:2

我有一个带有动物名称的数据框

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 中编写相当于“是……的子集”的代码,我相信这必须有一个简单的解决方案。非常感谢任何帮助

r if-statement vector logic logical-operators
2个回答
0
投票

您可以将元素放入命名列表中,然后

stack
:

lookup <- stack(dplyr::lst(carnivore, herbivore, omnivore))

或者像这样如果你想用

x
更新
match
:

x$diet <- lookup$ind[match(x$animal, lookup$values)]

#     animal      diet
# 1     lion carnivore
# 2    tiger carnivore
# 3      cow herbivore
# 4  vulture  omnivore
# 5    hyena  omnivore
# 6  leopard carnivore
# 7     deer herbivore
# 8 gazzelle herbivore

0
投票

我认为您正在寻找

%in%
运算符:

x$diet[x$animal %in% omnivore] <- "Omnivore"
x$diet[x$animal %in% carnivore] <- "Carnivore"
x$diet[x$animal %in% herbivore] <- "Herbivore"
© www.soinside.com 2019 - 2024. All rights reserved.