我想检查数据框的变量是否至少包含其中一个值。我想要OR函数的R等价物,看起来像这样(这里的OR函数显然是错误的)
lost$League =ifelse(grepl(OR("Academy","H.S.") , lost$Drafted.From), "Highschool","Not highschool")
lost$League
将有价值" Highschool"如果lost$Drafted.From
包含"学院"或者" H.S。",并且有价值"没有高中"否则。
我是R的新手,英语不是我的第一语言,所以如果我的问题不是很清楚,我很抱歉。
答案 0 :(得分:2)
这完全取决于单词“contains”的解释。如果Drafted.From
列仅包含单个字词或完整(完全)匹配,则垂直管道|
或%in%
运算符就足够了。否则,您将需要grepl
。
一些示例数据:
lost <- data.frame(drafted.from.1 = c('Academy','College','H.S.'),
drafted.from.2 = c('He studied at the Academy','She went to College','He attended Dartmore H.S.'))
对于drafted.from.1
列,您可以按如下方式使用%in%
:
ifelse(lost$drafted.from.1 %in% c("Academy","H.S."), "Highschool", "Not highschool")
给出了正确的结果:
[1] "Highschool" "Not highschool" "Highschool"
但grepl
也适用于这种情况:
> ifelse(grepl("Academy|H.S.", lost$drafted.from.1), "Highschool", "Not highschool")
[1] "Highschool" "Not highschool" "Highschool"
对于drafted.from.2
列,您需要grepl
分配Highschool
和Not highschool
值:
ifelse(grepl("Academy|H.S.", lost$drafted.from.2), "Highschool", "Not highschool")
给出了正确的结果:
[1] "Highschool" "Not highschool" "Highschool"
关于drafted.from.2
列,%in%
(或使用|
OR运算符)不会给出正确的结果:
> ifelse(lost$drafted.from.2 %in% c("Academy","H.S."), "Highschool", "Not highschool")
[1] "Not highschool" "Not highschool" "Not highschool"
现在让我们将这些知识应用于数据框:
lost$League.1 <- ifelse(lost$drafted.from.1 %in% c("Academy","H.S."), "Highschool", "Not highschool")
lost$League.2 <- ifelse(grepl("Academy|H.S.", lost$drafted.from.2), "Highschool", "Not highschool")
结果:
> lost
drafted.from.1 drafted.from.2 League.1 League.2
1 Academy He studied at the Academy Highschool Highschool
2 College She went to College Not highschool Not highschool
3 H.S. He finished his H.S. Highschool Highschool
答案 1 :(得分:0)
使用|
(或)运算符,如下所示:
mydf$lost <- ifelse(lost$Drafted.From == "Academy" | lost$Drafted.From == "H.S.", "Highschool", "Not highschool")