Re: [R] tapply, data.frame problem

From: Chuck Cleland <ccleland_at_optonline.net>
Date: Wed 17 Jan 2007 - 19:54:42 GMT

Lauri Nikkinen wrote:
> Hi R-users,
>
> I'm quite new to R and trying to learn the basics. I have a following
> problem concerning the convertion of array object into data frame. I have
> made following data sets
>
> tmp1 <- rnorm(100)
> tmp2 <- gl(10,2,length=100)
> tmp3 <- as.data.frame(cbind(tmp1,tmp2))
> tmp3.sum <- tapply(tmp3$tmp1,tmp3$tmp2,sum)
> tmp3.sum <- as.data.frame(tapply(tmp1,tmp2,sum))
> and I want the levels from tmp2 be shown as a column in the data.frame, not
> as row name as it now does. To put it in another way, as a result, I want a
> data frame with two columns: levels and the sums of those levels. Row names
> can be, for example, numbers from 1 to 10.

aggregate(tmp3[1], tmp3[2], sum)

   tmp2        tmp1
1     1  8.41550650
2     2  3.65831086
3     3 -0.26296334
4     4  3.45368671
5     5 -4.64383794
6     6  0.25640949
7     7  0.02832348
8     8 -0.03811150
9     9  1.41724121

10 10 -1.06780900

?aggregate

> -Lauri Nikkinen
> Lahti, Finland
>
> [[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.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

______________________________________________
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.
Received on Thu Jan 18 06:59:04 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 17 Jan 2007 - 21:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.