Dear R helpers I have a data which gives Month-wise and Rating-wise Rates. So the input file is something like month rating rate January AAA 9.04 February AAA 9.07 .......................................... .......................................... Decemeber AAA 8.97 January BBB 11.15 February BBB 11.13 January CCC 17.13 ............................................. ............................................ December CCC 17.56 and so on. My objective is to calculate Rating-wise mean rate, for which I have used rating_mean = tapply(rate, rating, mean) and I am getting following output> tapply(rate, rating, mean)AAA BBB CCC 9.1104 11.1361637 17.1606779 which is correct when compared with an excel output. However, I wish to have my output something like a data.frame (so that I should be able to save this output as csv file with respective headings and should be able to carry out further analysis) Rating Mean AAA 9.1104 BBB 11.1361637 CCC 17.1606779 Please guide as how should I achieve my output like this. Thanking in advance. Regards Vincy [[alternative HTML version deleted]]
On 2010-10-27 03:39, Vincy Pyne wrote:> Dear R helpers > > I have a data which gives Month-wise and Rating-wise Rates. So the input file is something like > > month rating rate > January AAA 9.04 > February AAA 9.07 > .......................................... > .......................................... > Decemeber AAA 8.97 > January BBB 11.15 > February BBB 11.13 > > > > January CCC 17.13 > ............................................. > ............................................ > December CCC 17.56 > > and so on. > > My objective is to calculate Rating-wise mean rate, for which I have used > > rating_mean = tapply(rate, rating, mean) > > and I am getting following output > >> tapply(rate, rating, mean) > AAA BBB CCC > 9.1104 11.1361637 17.1606779 > > which is correct when compared with an excel output. > > However, I wish to have my output something like a data.frame (so that I should be able to save this output as csv file with respective headings and should be able to carry out further analysis) > > Rating Mean > AAA 9.1104 > BBB 11.1361637 > CCC 17.1606779 > > > Please guide as how should I achieve my output like this.Something like this: rating <- gl(4, 12, labels = LETTERS[1:4]) rate <- rnorm(48, 10, 3) d <- data.frame(rating, rate) out <- aggregate(rate ~ rating, data = d, mean) -Peter Ehlers> > Thanking in advance. > > Regards > > Vincy > >
Or, tap <- tapply(rate, rating, mean) data.frame(Rating=as.factor(rownames(tap)), Mean=as.vector(tap)) remko -- View this message in context: http://r.789695.n4.nabble.com/One-silly-question-about-tapply-output-tp3015202p3015274.html Sent from the R help mailing list archive at Nabble.com.
dear Vincy, Firstly, a suggestion: to increase the probability of getting help, you should provide reproducible code (people can do "copy-and-paste" of your code and to modify the code to obtain the response.. ) However a possible solution (not tested, of course..) could be simply a<-tapply(rate, rating, mean) d<-data.frame(rating=names(a),mean=a) best, vito Il 27/10/2010 12.39, Vincy Pyne ha scritto:> Dear R helpers > > I have a data which gives Month-wise and Rating-wise Rates. So the input file is something like > > month rating rate > January AAA 9.04 > February AAA 9.07 > .......................................... > .......................................... > Decemeber AAA 8.97 > January BBB 11.15 > February BBB 11.13 > > > > January CCC 17.13 > ............................................. > ............................................ > December CCC 17.56 > > and so on. > > My objective is to calculate Rating-wise mean rate, for which I have used > > rating_mean = tapply(rate, rating, mean) > > and I am getting following output > >> tapply(rate, rating, mean) > AAA BBB CCC > 9.1104 11.1361637 17.1606779 > > which is correct when compared with an excel output. > > However, I wish to have my output something like a data.frame (so that I should be able to save this output as csv file with respective headings and should be able to carry out further analysis) > > Rating Mean > AAA 9.1104 > BBB 11.1361637 > CCC 17.1606779 > > > Please guide as how should I achieve my output like this. > > Thanking in advance. > > Regards > > Vincy > > > > > > > [[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.-- ===================================Vito M.R. Muggeo Dip.to Sc Statist e Matem `Vianelli' Universit? di Palermo viale delle Scienze, edificio 13 90128 Palermo - ITALY tel: 091 23895240 fax: 091 485726/485612 http://dssm.unipa.it/vmuggeo
Dear Sirs, Thanks a lot for your great help. This is going to help me immensely in future as many times I had found myself struggling with this problem. Thanks again for the great help. Regards Vincy --- On Wed, 10/27/10, Dimitri Liakhovitski <dimitri.liakhovitski@gmail.com> wrote: From: Dimitri Liakhovitski <dimitri.liakhovitski@gmail.com> Subject: Re: [R] One silly question about "tapply output" To: "Vincy Pyne" <vincy_pyne@yahoo.ca> Received: Wednesday, October 27, 2010, 11:28 AM Assign your result to an object and then write out the object as a csv file. For example: x<-data.frame(rating=rep(letters[1:3],2),rate=runif(1:6)) # example data frame rating.means<-tapply(x$rate,x$rating,mean) write.csv(rating.means,file="my.file.csv",row.names=T) Dimitri On Wed, Oct 27, 2010 at 6:39 AM, Vincy Pyne <vincy_pyne@yahoo.ca> wrote:> Dear R helpers > > I have a data which gives Month-wise and Rating-wise Rates. So the input file is something like > > month rating rate > January AAA 9.04 > February AAA 9.07 > .......................................... > .......................................... > Decemeber AAA 8.97 > JanuaryBBB 11.15> February BBB 11.13 > > > > January CCC 17.13 > ............................................. > ............................................ > December CCC 17.56 > > and so on. > > My objective is to calculate Rating-wise mean rate, for which I have used > > rating_mean = tapply(rate, rating, mean) > > and I am getting following output > >> tapply(rate, rating, mean) > AAABBB CCC> 9.1104 11.1361637 17.1606779 > > which is correct when compared with an excel output. > > However, I wish to have my output something like a data.frame (so that I should be able to save this output as csv file with respective headings and should be able to carry out further analysis) > > Rating Mean > AAA 9.1104 > BBB 11.1361637 > CCC 17.1606779 > > > Please guide as how shouldI achieve my output like this.> > Thanking in advance. > > Regards > > Vincy > > > > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >-- Dimitri Liakhovitski Ninah Consulting www.ninah.com [[alternative HTML version deleted]]