[R] Counting within groups / means by groups

David Studer studerov at gmail.com
Mon Nov 10 15:39:47 CET 2014


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]]



More information about the R-help mailing list