[R] Help in getting aggregated data
Hi team, I have the data of the form: > a<- data.frame(x=c(1,2,1,4,3), y=c(1,2,1,4,3), z=c(1,2,3,4,5)) I need the output of the form > b<- data.frame(x=c(1,2,3,4), y=c(1,2,3,4), z=(3,2,5,4) ) As you can see, the Z value contains the maximum for each of the (x,y) combinations. I used > c<-by(a$z, list(x=a$x, y=a$y), max) > c[,] y x1 2 3 4 1 3 NA NA NA 2 NA 2 NA NA 3 NA NA 5 NA 4 NA NA NA 4 Not sure If I have any standard function to convert this to the data frame that I need. Can you help me convert the last array into the data frame? If there is any other way apart from using the "by" function, please inform me regarding that as well. -- Deepak Manohar T __ R-help@stat.math.ethz.ch 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.
Re: [R] Help in getting aggregated data
Try this: > a x y z 1 1 1 1 2 2 2 2 3 1 1 3 4 4 4 4 5 3 3 5 > # create indices of groups > indices <- split(seq(nrow(a)), list(a$x, a$y), drop=TRUE) > combine <- lapply(indices, function(.ind){ + # create a row representing the max of z + c(x=a$x[.ind[1]], y=a$y[.ind[1]], z=max(a$z[.ind])) + }) > do.call('rbind', combine) # put back into a matrix x y z 1.1 1 1 3 2.2 2 2 2 3.3 3 3 5 4.4 4 4 4 > On 3/31/07, Deepak Manohar <[EMAIL PROTECTED]> wrote: > > Hi team, > I have the data of the form: > > > a<- data.frame(x=c(1,2,1,4,3), y=c(1,2,1,4,3), z=c(1,2,3,4,5)) > > I need the output of the form > > > b<- data.frame(x=c(1,2,3,4), y=c(1,2,3,4), z=(3,2,5,4) ) > > As you can see, the Z value contains the maximum for each of the (x,y) > combinations. > > I used > > c<-by(a$z, list(x=a$x, y=a$y), max) > > c[,] > y > x1 2 3 4 > 1 3 NA NA NA > 2 NA 2 NA NA > 3 NA NA 5 NA > 4 NA NA NA 4 > > Not sure If I have any standard function to convert this to the data > frame that I need. Can you help me convert the last array into the > data frame? If there is any other way apart from using the "by" > function, please inform me regarding that as well. > > -- Deepak Manohar T > > __ > R-help@stat.math.ethz.ch 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] Help in getting aggregated data
Try this: aggregate(a[3], a[1:2], max) On 3/31/07, Deepak Manohar <[EMAIL PROTECTED]> wrote: > Hi team, > I have the data of the form: > > > a<- data.frame(x=c(1,2,1,4,3), y=c(1,2,1,4,3), z=c(1,2,3,4,5)) > > I need the output of the form > > > b<- data.frame(x=c(1,2,3,4), y=c(1,2,3,4), z=(3,2,5,4) ) > > As you can see, the Z value contains the maximum for each of the (x,y) > combinations. > > I used > > c<-by(a$z, list(x=a$x, y=a$y), max) > > c[,] > y > x1 2 3 4 > 1 3 NA NA NA > 2 NA 2 NA NA > 3 NA NA 5 NA > 4 NA NA NA 4 > > Not sure If I have any standard function to convert this to the data > frame that I need. Can you help me convert the last array into the > data frame? If there is any other way apart from using the "by" > function, please inform me regarding that as well. > > -- Deepak Manohar T > > __ > R-help@stat.math.ethz.ch 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. > __ R-help@stat.math.ethz.ch 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.
Re: [R] Help in getting aggregated data
Both aggregate and custom functionality given by Jim works. Thanks On 4/1/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > Try this: > > aggregate(a[3], a[1:2], max) > > > On 3/31/07, Deepak Manohar <[EMAIL PROTECTED]> wrote: > > Hi team, > > I have the data of the form: > > > > > a<- data.frame(x=c(1,2,1,4,3), y=c(1,2,1,4,3), z=c(1,2,3,4,5)) > > > > I need the output of the form > > > > > b<- data.frame(x=c(1,2,3,4), y=c(1,2,3,4), z=(3,2,5,4) ) > > > > As you can see, the Z value contains the maximum for each of the (x,y) > > combinations. > > > > I used > > > c<-by(a$z, list(x=a$x, y=a$y), max) > > > c[,] > > y > > x1 2 3 4 > > 1 3 NA NA NA > > 2 NA 2 NA NA > > 3 NA NA 5 NA > > 4 NA NA NA 4 > > > > Not sure If I have any standard function to convert this to the data > > frame that I need. Can you help me convert the last array into the > > data frame? If there is any other way apart from using the "by" > > function, please inform me regarding that as well. > > > > -- Deepak Manohar T > > > > __ > > R-help@stat.math.ethz.ch 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. > > > -- Deepak Manohar T Trilogy 09342889008 __ R-help@stat.math.ethz.ch 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.