Rilcon42
Rilcon42

Reputation: 9763

getting means of matched columns in different dataframes

How can I get the means of 3 different dataframes? I tried:

df1<-data.frame(c(1,2,3),c(1,7,3),c(rep(1,3)))
colnames(df1)<-c("c1","c2","c3")
df2<-data.frame(c(5,5,5),c(1,3,4),c(rep(1,3)))
colnames(df2)<-c("c1","c2","c3")
df3<-data.frame(c(2,5,2),c(1,1,1),c(rep(1,3)))
colnames(df3)<-c("c1","c2","c3")
#m3<-m3[!(is.na(m3[,3])|is.na(m3[,4])),]
#m3[!duplicated(m3$c1),]

cbind(apply(data.frame(df1,df2,df3), 1, mean))

which gave me the means of everything in the row. My goal is to get the means of the matching row eg mean of (df1[,1] and df2[,1] and df[,3]) for each column.

What I was hoping for is:

      c1.mean    c2.mean   c3.mean
row1    2.67         1       1
row2      4         3.67     1
row3    3.33        2.67    1

Upvotes: 0

Views: 40

Answers (2)

akrun
akrun

Reputation: 887691

If there are more datasets, we can place them in a list, + with Reduce and divide by the n number of datasets.

setNames(round(Reduce('+', mget(ls(pattern='df\\d+')))/3,2),
                          paste0(names(df1), '.mean'))
#  c1.mean c2.mean c3.mean
#1    2.67    1.00       1
#2    4.00    3.67       1
#3    3.33    2.67       1

The above method may not be the best when you have NAs in the dataset. If there are NA values, we could convert the list to array and use apply with rowMeans and na.rm=TRUE

res <- round(apply(array(unlist(mget(ls(pattern='df\\d+'))), 
               dim=c(dim(df1), 3)), 2, rowMeans, na.rm=TRUE),2)
res 
#     [,1] [,2] [,3]
#[1,] 2.67 1.00    1
#[2,] 4.00 3.67    1
#[3,] 3.33 2.67    1

colnames(res) <- paste0(names(df1), '.mean')

Upvotes: 0

Shenglin Chen
Shenglin Chen

Reputation: 4554

setNames(round((df1+df2+df3)/3,digit=2),paste0('c',1:3,'.mean'))

Upvotes: 2

Related Questions