R Given a list of identical tables of measurement data, output a summary of the means of each cell

I find it difficult to put what I want words to, so I will try to follow an example to explain this. Let's say I repeated the experiment twice and had two tables:

[df1]   [df2]
 X Y     X Y
 2 3     4 1
 5 2     2 4

These tables are stored in the list (if the list can contain more than two elements, if necessary), and I want to create an average value for each cell in the tables in the list (or for the generalized version, use any function that I select for the cells, i.e. . mad, sd, etc.)

[df1]   [df2]         [dfMeans]
 X Y     X Y        X          Y
 2 3     4 1     mean(2,4) mean(3,1)
 5 2     2 4     mean(5,2) mean(2,4)

I have a code solution for my problem, but since this is in R, this is most likely a cleaner way to do something:

df1 <- data.frame(X=c(2,3,4),Y=c(3,2,1))
df2 <- data.frame(X=c(5,1,3),Y=c(4,1,4))
df3 <- data.frame(X=c(2,7,4),Y=c(1,7,6))
dfList <- list(df1,df2,df3)

dfMeans <- data.frame(MeanX=c(NA,NA,NA),MeanY=c(NA,NA,NA))

for (rowIndex in 1:nrow(df1)) {
  for (colIndex in 1:ncol(df1)) {
    valuesAtCell <- c()
    for (tableIndex in 1:length(dfList)) {
      valuesAtCell <- c(valuesAtCell, dfList[[tableIndex]][rowIndex,colIndex])
    }
    dfMeans[rowIndex, colIndex] <- mean(valuesAtCell)
  }
}

print(dfMeans)
+4
source share
2

data.table, :

library(data.table)

dtList <- rbindlist(dfList, use.names = TRUE, idcol = TRUE)
dtList
   .id X Y
1:   1 2 3
2:   1 3 2
3:   1 4 1
4:   2 5 4
5:   2 1 1
6:   2 3 4
7:   3 2 1
8:   3 7 7
9:   3 4 6

dtList[, rn := 1:.N, by = .id][][, .(X = mean(X), Y = mean(Y)), by = rn]
   rn        X        Y
1:  1 3.000000 2.666667
2:  2 3.666667 3.333333
3:  3 3.666667 3.666667

mean , , median. .id , .

Edit

( , ):

cn <- colnames(df1)
cn
[1] "X" "Y"

dtList[, rn := 1:.N, by = .id][, lapply(.SD, mean), by = rn, .SDcols = cn][, rn := NULL][]
          X        Y
1: 3.000000 2.666667
2: 3.666667 3.333333
3: 3.666667 3.666667

, . [, rn := NULL] , [] , .

+3

data.frame list Reduce() dfList, df, .

Reduce(`+`, dfList) / length(dfList)
#         X        Y
#1 3.000000 2.666667
#2 3.666667 3.333333
#3 3.666667 3.666667
+3

Source: https://habr.com/ru/post/1648466/


All Articles