[R] Counting within groups / means by groups

David L Carlson dcarlson at tamu.edu
Mon Nov 10 16:51:52 CET 2014


In addition to Jeff's recommendation, you need to read a basic introduction to R. Your data frame is probably not what you think it is:

> group<-c("A", "A", "A", "B", "B", "B", "B", "C")
> value<-c(1,3,2,2,2,4,4,1)
> df<-as.data.frame(cbind(group, value))
> str(df)
'data.frame':   8 obs. of  2 variables:
 $ group: Factor w/ 3 levels "A","B","C": 1 1 1 2 2 2 2 3
 $ value: Factor w/ 4 levels "1","2","3","4": 1 3 2 2 2 4 4 1

By using cbind() you combined a character vector and a numeric vector into a matrix so R converted the numeric value to characters since a matrix can hold only a single data type. The cbind() function is generic and which version you get depends on the first argument.
> cbind(group, value)
     group value
[1,] "A"   "1"  
[2,] "A"   "3"  
[3,] "A"   "2"  
[4,] "B"   "2"  
[5,] "B"   "2"  
[6,] "B"   "4"  
[7,] "B"   "4"  
[8,] "C"   "1"  

Then you used as.data.frame() to convert the character matrix to a data.frame. The default for character variables is to convert those to factors. All you need is
> dfa <- data.frame(group, value)
> str(dfa)
'data.frame':   8 obs. of  2 variables:
 $ group: Factor w/ 3 levels "A","B","C": 1 1 1 2 2 2 2 3
 $ value: num  1 3 2 2 2 4 4 1

I changed df to dfa since df() is the density function for the f distribution. R is not likely to get confused, but you might.

Then read the manual page on ave() to see why these work and how to adapt them:

> ave(dfa$value, dfa$group, FUN=length)
[1] 3 3 3 4 4 4 4 1
> ave(dfa$value, dfa$group)
[1] 2 2 2 3 3 3 3 1

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff Newmiller
Sent: Monday, November 10, 2014 9:19 AM
To: studerov at gmail.com; r-help at r-project.org
Subject: Re: [R] Counting within groups / means by groups

Help file ?ave should apply here.

Please read the Posting Guide mentioned in the footer of every email on this list and on the list manager page for this mailing list. It warns you to read the archives before posting and to post in plain text format rather than HTML format.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        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 November 10, 2014 6:39:47 AM PST, David Studer <studerov at gmail.com> wrote:
>Hi everyone!
>
>I have problems finding a solution to the following two problems:
>
>My sample-dataframe consists of two variables "group" and "value":
>
>group<-c("A", "A", "A", "B", "B", "B", "B", "C")
>value<-c(1,3,2,2,2,4,4,1)
>df<-as.data.frame(cbind(group, value))
>
>Problem 1:
>**********
>
>Now I'd like to count the number of group-A-cases, group-B-cases etc
>and
>write
>this number into a new column. It should be like:
>
>count_group<-c(3, 3, 3, 4, 4, 4, 4, 1)
>
>Problem 2:
>***********
>
>I'd like to add new column with the mean values (or any other function)
>within
>my groups. E.g:
>
>Group A: (1+3+2)/3=2
>Group B: (2+2+4+4)/4=3
>Group C: =1
>
>Now I'd add another column 2 2 3 3 3 3 1
>
>
>Can anyone help me, how this can be done best?
>
>Thank you!
>David
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at 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.

______________________________________________
R-help at 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.



More information about the R-help mailing list