Re: [R] aggregating variables (sum within groups)

2015-02-26 Thread William Dunlap
Even though I was looking in several r-books I could not find a suitable function to this problem Which R books did you look through? Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Feb 26, 2015 at 4:02 AM, David Studer stude...@gmail.com wrote: Hello everybody! I have a (probabely

Re: [R] aggregating variables (sum within groups)

2015-02-26 Thread Jeff Newmiller
For the record, the ave function in R can apply any function you specify, not just mean. The primary feature of ave is that it does not collapse the rows like aggregate does. Choose among them according to how you want the output to be organized.

[R] aggregating variables (sum within groups)

2015-02-26 Thread David Studer
Hello everybody! I have a (probabely very easy) problem. Even though I was looking in several r-books I could not find a suitable function to this problem, that's why I hope that someone here could help me: # Sample data: group-c(A,A,A,B,B,C,C,C) var1-c(1,0,0,1,1,0,NA,1) var2-c(0,1,NA,0,1,1,0,0)

Re: [R] aggregating variables (sum within groups)

2015-02-26 Thread Ivan Calandra
Hi David, You have your answer in the question: aggregate() aggregate(cbind(var1,var2)~group, data=testdata, FUN=sum) Although I am not sure what you intended to do with testdata$x- as the result cannot have the same number of rows than testdata HTH, Ivan -- Ivan Calandra, ATER University

Re: [R] aggregating variables (sum within groups)

2015-02-26 Thread Brandstätter Christian
Dear David, your email is quite confusing. Do you want to get the sum for each group (A,B,C) or each variable as would be indicated by your result? sum by group: aggregate(data=testdata,var1~group,sum) count by group: aggregate(data=testdata,var1~group,length) sum by variable: