Either
Dat$c <- ifelse( "Y"==Dat$b, rep( "D", nrow(Dat) ), Dat$a )
Or (more efficiently)
Dat$c <- Dat$a
Dat$c[ "Y"==Dat$b ] <- "D"
Also, beware of creating data frames without using the stringsAsFactors=FALSE
option if you plan to replace levels like this.
-
?ifelse
I changed Dat to a. have character type instead of factor, and b.
actually match what you show in your example instead of already having
D.
Dat <- data.frame(a= c('A','A','C','B','C','C','B'),
b= c('N','N','Y','N','Y','N','N'), stringsAsFactors=FALSE )
Dat$c <- with(Dat, ifelse(b == "Y"
Hello all,
I have a rather easy question. I want to add a column to the database which
will change the values of vector a based on the values to vector b. Any
ideas how?
For example:
Dat <- data.frame(a= c('A','A','C','B','D','D','B'),
b= c('N','N','Y
3 matches
Mail list logo