[R] One silly question about "tapply output"

Peter Ehlers ehlers at ucalgary.ca
Wed Oct 27 13:33:21 CEST 2010


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
>
>



More information about the R-help mailing list