[R] Odp: Grouping by interval

Petr PIKAL petr.pikal at precheza.cz
Fri Dec 7 11:27:57 CET 2007


Hi

r-help-bounces at r-project.org napsal dne 07.12.2007 10:04:35:

> Hello,
> 
> I have a dataframe of say 20 lines with one line per individual. I want 
to 
> group these 20 individuals
> by length class (eg. of 5cm) and get the mean value of all the other 
variables
> (eg VarA and VarB) for each length class
> 
> My dataframe is as follow:
> 
> Length <- 10:30
> VarA <- seq(1000,1200,10)
> VarB <- seq(500,700,10)
> Data <- cbind(Length,VarA,VarB)
> 
> 
> And I want to get something like:
> 
> 
> Length Class      Mean VarA       Mean VarB
> [10-15[               1020                520
> [15-20[               1070                570
> [20-25[               1120                620
> [25-30]               1175                675
> 
> 
> Would you have any suggestions how to do that ?
> Many thanks.

Cut and aggregate

fac <- cut(Data[,1], seq(5,30,5))

> aggregate(Data[,-1],list(fac), mean)
  Group.1 VarA VarB
1  (5,10] 1000  500
2 (10,15] 1030  530
3 (15,20] 1080  580
4 (20,25] 1130  630
5 (25,30] 1180  680

is close, but you need to add first level in fac into the second.

lev<-levels(fac)
levels(fac)<-lev[c(2,2,3,4,5)]

> aggregate(Data[,-1],list(fac), mean)
  Group.1 VarA VarB
1 (10,15] 1025  525
2 (15,20] 1080  580
3 (20,25] 1130  630
4 (25,30] 1180  680

Not sure if it is the most elegant solution but it works.

Regards
Petr



> 
> 
> Sonia.
> 
>    [[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