Home > Software design >  How to calculate rowMeans for dataframes in a list?
How to calculate rowMeans for dataframes in a list?

Time:06-22

I have a list (my.list) that looks like this:

> my.list
$S1
  A B C D
1 5 2 3 2
2 6 3 4 3
3 7 5 5 5
4 2 3 6 7
5 6 6 7 6

$S2
  A B C D
1 5 2 3 2
2 6 3 4 3
3 7 5 5 5
4 2 3 6 7
5 6 6 7 6

I want to create a new variable, 'E', by taking the row means of columns A-D.

I've tried using both a loop:

test_list<-list()

for(i in 1:5){
  test_data$E <- list.append(test_list, mylist[[i]] %>% rowMeans(mylist[,c("A","B","C","D")]))
}

and lapply:


merged_data <- lapply(merged_data, transform, E = rowMeans(mylist[,c("A","B","C","D")]))

And both do not seem to be working. I get the error message:

Error in mylist[c("A","B","C","D"),  : 
  incorrect number of dimensions

How can I go about doing this?

Reproducible Data:

my.list <- structure(list(S1 = structure(list(A = c(5, 6, 7, 2, 6), B = c(2,3,5,3,6), C = c(3,4,5,6,7), D = c(2,3,5,7,6)),.Names = c("A", "B", "C", "D"), class = "data.frame", row.names = c("1","2", "3", "4", "5")), S2 = structure(list(A = c(5, 6, 7, 2, 6), B = c(2,3,5,3,6), C = c(3,4,5,6,7), D = c(2,3,5,7,6)), .Names = c("A", "B", "C", 
"D"), class = "data.frame", row.names = c("1", "2", "3", "4","5"))), .Names = c("S1", "S2"))

CodePudding user response:

use lapply:

lapply(my.list, \(x)transform(x, E = rowMeans(x)))

or even

lapply(my.list, \(x)cbind(x, E = rowMeans(x)))

both results in:

$S1
  A B C D    E
1 5 2 3 2 3.00
2 6 3 4 3 4.00
3 7 5 5 5 5.50
4 2 3 6 7 4.50
5 6 6 7 6 6.25

$S2
  A B C D    E
1 5 2 3 2 3.00
2 6 3 4 3 4.00
3 7 5 5 5 5.50
4 2 3 6 7 4.50
5 6 6 7 6 6.25

CodePudding user response:

additional option

library(tidyverse)
map(my.list, ~mutate(.x, E = rowMeans(.x)))

$S1
  A B C D    E
1 5 2 3 2 3.00
2 6 3 4 3 4.00
3 7 5 5 5 5.50
4 2 3 6 7 4.50
5 6 6 7 6 6.25

$S2
  A B C D    E
1 5 2 3 2 3.00
2 6 3 4 3 4.00
3 7 5 5 5 5.50
4 2 3 6 7 4.50
5 6 6 7 6 6.25
  • Related