aggregating variables (sum within groups)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

aggregating variables (sum within groups)

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)
testdata<-data.frame(group, var1, var2)

Now, I'd like to generate two aggregated variables:

testdata$x<- ???   should count the sum of var1 within each group (=4)
testdata$y<- ???   should count the sum of var2 within each group (=3)

Therefore I am looking for a function like ave() which does not calculate
the mean value but a sum.

Thank you for any hints!

David

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: aggregating variables (sum within groups)

Ivan Calandra-4
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 of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
[hidden email]
https://www.researchgate.net/profile/Ivan_Calandra

Le 26/02/15 13:02, David Studer a écrit :

> 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)
> testdata<-data.frame(group, var1, var2)
>
> Now, I'd like to generate two aggregated variables:
>
> testdata$x<- ???   should count the sum of var1 within each group (=4)
> testdata$y<- ???   should count the sum of var2 within each group (=3)
>
> Therefore I am looking for a function like ave() which does not calculate
> the mean value but a sum.
>
> Thank you for any hints!
>
> David
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: aggregating variables (sum within groups)

Christian Brandstätter
In reply to this post by David Studer
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:
sum(na.omit(testdata$var1))

But homework shouldn't be posted on this list.
Best regards
Christian



2015-02-26 13:02 GMT+01:00 David Studer <[hidden email]>:

> 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)
> testdata<-data.frame(group, var1, var2)
>
> Now, I'd like to generate two aggregated variables:
>
> testdata$x<- ???   should count the sum of var1 within each group (=4)
> testdata$y<- ???   should count the sum of var2 within each group (=3)
>
> Therefore I am looking for a function like ave() which does not calculate
> the mean value but a sum.
>
> Thank you for any hints!
>
> David
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: aggregating variables (sum within groups)

Jeff Newmiller
In reply to this post by David Studer
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.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On February 26, 2015 4:02:49 AM PST, David Studer <[hidden email]> wrote:

>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)
>testdata<-data.frame(group, var1, var2)
>
>Now, I'd like to generate two aggregated variables:
>
>testdata$x<- ???   should count the sum of var1 within each group (=4)
>testdata$y<- ???   should count the sum of var2 within each group (=3)
>
>Therefore I am looking for a function like ave() which does not
>calculate
>the mean value but a sum.
>
>Thank you for any hints!
>
>David
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 | Threaded
Open this post in threaded view
|

Re: aggregating variables (sum within groups)

William Dunlap
In reply to this post by David Studer
> 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 <[hidden email]> wrote:

> 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)
> testdata<-data.frame(group, var1, var2)
>
> Now, I'd like to generate two aggregated variables:
>
> testdata$x<- ???   should count the sum of var1 within each group (=4)
> testdata$y<- ???   should count the sum of var2 within each group (=3)
>
> Therefore I am looking for a function like ave() which does not calculate
> the mean value but a sum.
>
> Thank you for any hints!
>
> David
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.