[R] How to recode variables using base R
johannes rara
johannesraja at gmail.com
Tue Mar 30 15:14:46 CEST 2010
Thanks John and Henrique, my intention is to do this for A, B and C
(all at once), so I'll have to wrap your solution into lapply or for
loop?
-J
2010/3/30 Henrique Dallazuanna <wwwhsd at gmail.com>:
> 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