Skip to content
Prev 306308 / 398506 Next

create new column in a DF according to values from another column

Hello,

'if' is not vectorized, it only uses the first value of the multiple 
condition. 'ifelse' is vectorized and in your case use nested ifelses.


DF <- data.frame(number=c(1,4,7,3,11,16,14,17,20,19),data=c(1:10))
v1 <- c(1,7,11,16)
v2 <- c(4,14,20)
v3 <- c(3,17,19)

DF$Station <- ifelse(DF$number %in% v1, "V1",
             ifelse(DF$number %in% v2, "V2", "V3"))


Hope this helps,

Rui Barradas

Em 26-09-2012 11:49, jeff6868 escreveu: