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. Sonia. [[alternative HTML version deleted]]
Sonia Mehault pisze:> 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. > > > Sonia. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help w 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. >Sonia! use cut fuction. Create two wectos with intervals and lables acording documentation. Very simple and fast solution. Jarek
Try this: Length <- 10:30 VarA <- seq(1000,1200,10) VarB <- seq(500,700,10) Data <- cbind(Length,VarA,VarB) # create a factor of desired Length classes and aggregate classLength=cut(Length,c(10,15,20,25,30),include.lowest=TRUE) aggregate(cbind(VarA,VarB),by=list(LengthClass=classLength),FUN=mean) LengthClass VarA VarB 1 [10,15] 1025 525 2 (15,20] 1080 580 3 (20,25] 1130 630 4 (25,30] 1180 680 -- Andrej Blejec National Institute of Biology Vecna pot 111 POB 141 SI-1000 Ljubljana SLOVENIA e-mail: andrej.blejec at nib.si URL: http://ablejec.nib.si tel: + 386 1 423 33 88 fax: + 386 1 241 29 80 -------------------------- Organizer of Applied Statistics 2008 conference http://ablejec.nib.si/AS2008> -----Original Message----- > From: r-help-bounces at r-project.org[mailto:r-help-bounces at r-project.org]> On Behalf Of Sonia Mehault > Sent: Friday, December 07, 2007 10:05 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Grouping by interval > > Hello, > > I have a dataframe of say 20 lines with one line per individual. Iwant 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. > > > 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.
Try this: Vec <- cut(Data[,1], breaks=c(10,15,20,25,30), include=T, right=F) Data <- data.frame(Data, Vec) aggregate(Data[,2:3], list(Data$Vec), mean) On 07/12/2007, Sonia Mehault <mehault at iim.csic.es> wrote:> 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. > > > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
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 wantto> group these 20 individuals > by length class (eg. of 5cm) and get the mean value of all the othervariables> (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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Check the cut() function. b On Dec 7, 2007, at 4:04 AM, "Sonia Mehault" <mehault at iim.csic.es> wrote:> 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. > > > 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.
Reasonably Related Threads
- Automated generation of combinations
- How to loop through all the columns in dataframe
- How to pass in a list of variables as an argument to a function?
- Coefficient names when using lm() with contrasts
- R code for var-cov matrix given variances and correlations