Jazzmine - 1 year ago 131
R Question

# R use if else logic to assign value to new column

I need to create a new column that is based on one of three possible values found in a column.

These are the rules:

``````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"
``````

Here's my sample code

``````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"
}
)
``````

This is the data I want for each of the rows based upon the sample data for myLetters\$letterStatus:

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

Currently I'm getting "first" "first" "first" but I don't understand why.

Do you know how I can resolve this and why I got first for each row?

Thanks

using vectorisation (R's gift to us) to obtain the results :

``````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"))
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download