R使用if else逻辑将值赋给新列

时间:2016-11-06 18:36:33

标签: r substring sapply

我需要创建一个基于列中三个可能值之一的新列。

这些是规则:

If it has c somewhere in it, the new column should be assigned "third"
If it has b, but not c somewhere in it, the new column should be assigned "second"
If it has a but not b or c somewhere in it, the new column should be assigned "first"

这是我的示例代码

x <- c('a,b,c', 'a', 'a,b')

myLetters <- data.frame(x)

setnames(myLetters, "theLetter") 

sapply(myLetters$, theLetter, function(x) 
if ('c' %in% myLetters$theLetter) {
    myLetters$letterStatus <- "third"
} else if ('b' %in% myLetters$theLetter) {
    myLetters$letterStatus <- "second" 
} else if ('a'  %in% myLetters$theLetter) {
    myLetters$letterStatus <- "first"
}
)

这是我想要的每个行的数据,基于myLetters $ letterStatus的示例数据:

Row 1: third
Row 2: first
Row 3: second

目前我正在“先”“先”“先”,但我不明白为什么。

你知道我怎么解决这个问题以及为什么我每排第一个?

由于

1 个答案:

答案 0 :(得分:0)

使用矢量化(R&#39; s给我们的礼物)来获得结果:

x <- c('a,b,c', 'a', 'a,b')
myLetters <- data.frame(x)
# myLetters
# x
# 1 a,b,c
# 2     a
# 3   a,b

myLetters$x1 = ifelse(grepl("c",myLetters$x), "third", ifelse(grepl("b",myLetters$x),"second", "first"))