[R] How to recode variables using base R

Henrique Dallazuanna wwwhsd at gmail.com
Tue Mar 30 14:44:39 CEST 2010


You could try this also:

cut(df$A, c(-Inf, 3, 8), labels = c('x', 'y'))

On Tue, Mar 30, 2010 at 8:30 AM, johannes rara <johannesraja at gmail.com> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list