On Feb 25, 2011, at 10:14 AM, zem wrote:


Yeah, you are right
i want to post an short example what i want to do .. and in the meantime i
solved the problem ...
but here is:
i have something like this dataframe:
c1<-c(1,2,3,2,2,3,1,2,2,2)
c2<-c(5,6,7,7,5,7,5,7,6,6)
c3<-rnorm(10)
x<-cbind(c1,c2,c3)
x
     c1 c2          c3
[1,]  1  5  0.08279036
[2,]  2  6  0.59135988
[3,]  3  7  1.45520468
[4,]  2  7 -1.70094640
[5,]  2  5  0.13065228
[6,]  3  7 -1.12080980
[7,]  1  5  0.42779354
[8,]  2  7 -1.53111972
[9,]  2  6  0.29299987
[10,]  2  6 -0.01602095

#whith aggregate i receive this:
aggregate(x[,3],list(x[,1],x[,2]),mean)
 Group.1 Group.2          x
1       1       5  0.2552920
2       2       5  0.1306523
3       2       6  0.2894463
4       2       7 -1.6160331
5       3       7  0.1671974


and the problem was that i was grouping by 2 columns, so i couldn't copy the
result to x.

the solution was i made another column with paste(x[,1],x[,2],sep="_")
and then i used the solution from this link:
http://tolstoy.newcastle.edu.au/R/help/06/07/30184.html
so i solved my problem

Right. That works and has the virtue that it is reasonably clear what is going on. Another approach, possibly even more clear and even more R-ish, is to use the interaction() function.

> aggregate(x[,3], list(interaction(x[,1],x[,2]) ), mean)
  Group.1            x
1     1.5 -0.658932424
2     2.5  0.824756795
3     2.6  0.640471421
4     2.7 -0.008519716
5     3.7 -0.053233855



Ivan, many thanks for your support and quik responses! :)

--
View this message in context: 
http://r.789695.n4.nabble.com/group-by-in-data-frame-tp3324240p3324608.html
Sent from the R help mailing list archive at Nabble.com.

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

David Winsemius, MD
West Hartford, CT

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

Reply via email to