[R] How to recode variables using base R

johannes rara johannesraja at gmail.com
Tue Mar 30 13:30:47 CEST 2010


Hi,

Is there an efficient way recoding variables in a data.frame using
base R? My purpose is to create
new variables and attach them into old data.frame. The basic idea is
shown below, but how to create recoding for A, B and C and assing them
into new variables?

df <- data.frame(A = c(1:5),
B = c(3,6,2,8,10),
C = c(0,15,5,9,12))

df$A[df$A <= 3] <- "x"
df$A[df$A > 3 & df$A <= 8] <- "y"
df$A[df$A <= 16] <- "z"

Thanks,
-J



More information about the R-help mailing list