maycca
maycca

Reputation: 4090

Get the mean across list of dataframes by rows

I have a list of dataframes and I want to calculate a mean from each first rows, for all second rows etc.

I think this is possible by creating some common factor as index, put dataframes together using rbind and then calculate the mean value using aggregate(value ~ row.index, mean, large.df). However, I guess there is more straightforward way?

Here is my example:

df1 = data.frame(val = c(4,1,0))
df2 = data.frame(val = c(5,2,1))
df3 = data.frame(val = c(6,3,2))

myLs=list(df1, df2, df3)

[[1]]
  val
1   4
2   1
3   0

[[2]]
  val
1   5
2   2
3   1

[[3]]
  val
1   6
2   3
3   2

And my expected dataframe output, as rowise means:

df.means
  mean
1   5
2   2
3   1

My first steps, not working as expected yet:

# Calculate the mean of list by rows
lapply(myLs, function(x) mean(x[1,]))

Upvotes: 2

Views: 802

Answers (4)

tmfmnk
tmfmnk

Reputation: 40091

Another base R possibility could be:

Reduce("+", myLs)/length(myLs)

  val
1   5
2   2
3   1

Upvotes: 0

zx8754
zx8754

Reputation: 56219

Using double loop:

sapply(1:3, function(i) mean(sapply(myLs, function(j) j[i, ] )))
# [1] 5 2 1

Upvotes: 0

ThomasIsCoding
ThomasIsCoding

Reputation: 102241

Here is another base R solution using unlist + data.frame + rowMeans, i.e.,

rowMeans(data.frame(unlist(myLs,recursive = F)))
# [1] 5 2 1

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389135

A simple way would be to cbind the list and calculate mean of each row with rowMeans

rowMeans(do.call(cbind, myLs))
#[1] 5 2 1

We can also use bind_cols from dplyr to combine all the dataframes.

rowMeans(dplyr::bind_cols(myLs))

Upvotes: 3

Related Questions