1

I have this data frame (adjusted from @Vinterwoo's code), and I'm looking for a function to get the mean per group for each column. So the mean for group A in column C1 and C2, and the same for group B etcetera. I know how to get the mean per group (e.g. using aggregate) but I need the mean to show in every row of the corresponding group (please see desired output).

C1 <- c(3,2,4,3,6,7,5)
C2 <- c(3,7,3,4,5,2,1)
DF <- data.frame(ID=c("A","C","A","C","E","F","E"),C1=C1,C2=C2)

ID C1 C2
A  3  3
C  2  7
A  4  3
C  3  4
E  6  5
F  7  2
E  5  1

Desired output:

ID C1 C2 avg.C1 avg.C2
A  3  3    3.5    3.0
C  2  7    2.5    5.5
A  4  3    3.5    3.0
C  3  4    2.5    5.5
E  6  5    5.5    3.0
F  7  2    7.0    2.0
E  5  1    5.5    3.0

5 Answers 5

4

I would suggest the "data.table" package for this:

sdcols <- names(DF)[-1]      ## A vector of the new columns we want to add
as.data.table(DF)[, paste(sdcols, "mean", sep = "_") := lapply(.SD, mean), 
                  by = ID][] ## you can also be more specific and specify sdcols
#    ID C1 C2 C1_mean C2_mean
# 1:  A  3  3     3.5     3.0
# 2:  C  2  7     2.5     5.5
# 3:  A  4  3     3.5     3.0
# 4:  C  3  4     2.5     5.5
# 5:  E  6  5     5.5     3.0
# 6:  F  7  2     7.0     2.0
# 7:  E  5  1     5.5     3.0

As indicated in the commented code, you can specify which columns to act on using the .SDcols argument:

sdcols <- names(DF)[-1]
as.data.table(DF)[, paste(sdcols, "mean", sep = "_") := lapply(.SD, mean), 
                  by = ID, .SDcols = sdcols][]
2

Try:

library(dplyr)
DF %>% group_by(ID) %>% mutate(avg.C1 = mean(C1), avg.C2 = mean(C2))

Which gives:

#Source: local data frame [7 x 5]
#Groups: ID
#
#  ID C1 C2 avg.C1 avg.C2
#1  A  3  3    3.5    3.0
#2  C  2  7    2.5    5.5
#3  A  4  3    3.5    3.0
#4  C  3  4    2.5    5.5
#5  E  6  5    5.5    3.0
#6  F  7  2    7.0    2.0
#7  E  5  1    5.5    3.0
1

You can use aggregate and merge as the following

DF2=aggregate(cbind(C1, C2) ~ ID , data= DF , FUN= mean)
DF_Wanted= merge(DF, DF2, by=c("ID"), all=TRUE)
0

If you're going to create a new column using base you can simply calculate the desired values and assign them to the columns. To calculate that, you take the mean of each C that has ID equal to its own:

DF$avg.C1 <- sapply(1:nrow(DF), function(i) mean(DF$C1[DF$ID==DF$ID[i]]))
DF$avg.C2 <- sapply(1:nrow(DF), function(i) mean(DF$C2[DF$ID==DF$ID[i]]))
0

There are some good answers already posted, but I'm surprised no one mentioned ave(), which is basically designed for this exact purpose; it even runs mean() without any prodding!

cbind(DF,avg.C1=ave(DF$C1,DF$ID),avg.C2=ave(DF$C2,DF$ID));
##   ID C1 C2 avg.C1 avg.C2
## 1  A  3  3    3.5    3.0
## 2  C  2  7    2.5    5.5
## 3  A  4  3    3.5    3.0
## 4  C  3  4    2.5    5.5
## 5  E  6  5    5.5    3.0
## 6  F  7  2    7.0    2.0
## 7  E  5  1    5.5    3.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.