[R] cut and re-factor data

johannes rara johannesraja at gmail.com
Tue Sep 22 20:40:24 CEST 2009


Try e.g. function recode from package car.

2009/9/22 Chris Hane <christopher.a.hane at gmail.com>:
> Hello R-users,
> I have a data frame with a factor of ages in 5 year increments, and various
> count data for each age group. I only have this summary information in R at
> the moment.
>
> I want to create a new factor that aggregates the age factors if the
> existing factors have insufficient counts.  Then I can use aggregate to
> build a new data set.
>
> I figured out I can get the cut values I want using cut2 from Hmisc
>
>>count
>  [1] 10 20 20 50 50 50 50 50 50 50 50 50 50 50 10 20 20
>>age_bucket <- seq (0,80,5)
>>age_f <- factor(age_bucket)
>> age_f
>  [1] 0  10 15 20 25 30 35 40 45 5  50 55 60 65 70 75 80
> Levels: 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80
>  ct <- rep(age_bucket, count)
> cut50 <- cut2(ct,m=50)
> levels(cut50)
> [1] "[ 0,15)" "15"      "20"      "25"      "30"      "35"      "40"
> "45"      "[50,75]"
>
> Now I want to map age_f levels 1, 2 and 3 to cut50 level 1 on the data frame
> of count and age_bucket.
>
> Thanks in advance,
> Chris
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list