Home > Enterprise >  Calculate Mean for elements of a list
Calculate Mean for elements of a list

Time:03-11

I have a list of elements including five r coeffients for each gene:

my_list <- list(ENSG00000141956 = list(structure(0.158584641439316, .Dim = c(1L, 
1L), .Dimnames = list("expr", NULL)), structure(0.351303636855506, .Dim = c(1L, 
1L), .Dimnames = list("expr", NULL)), structure(0.144128203828052, .Dim = c(1L, 
1L), .Dimnames = list("expr", NULL)), structure(0.276265507681158, .Dim = c(1L, 
1L), .Dimnames = list("expr", NULL)), structure(-0.1854938275357, .Dim = c(1L, 
1L), .Dimnames = list("expr", NULL))), ENSG00000141959 = list(
    structure(0.101822670837826, .Dim = c(1L, 1L), .Dimnames = list(
        "expr", NULL)), structure(0.157722970392112, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(-0.0370731638581523, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(-0.176797462573245, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(-0.0687982984906863, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL))), ENSG00000142149 = list(
    structure(-0.0736461404779602, .Dim = c(1L, 1L), .Dimnames = list(
        "expr", NULL)), structure(0.0180832901610758, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(0.39674771703282, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(-0.147951509051988, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(0.192000437181621, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL))), ENSG00000142156 = list(
    structure(0.1121937808055, .Dim = c(1L, 1L), .Dimnames = list(
        "expr", NULL)), structure(-0.0358238958488585, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(-0.240240771420854, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(0.0834552485519515, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(-0.118048173374175, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL))), ENSG00000142166 = list(
    structure(-0.0440487643391083, .Dim = c(1L, 1L), .Dimnames = list(
        "expr", NULL)), structure(0.0419724287143289, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(0.155525788062941, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(0.195745293912149, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL)), structure(0.0197319683761103, .Dim = c(1L, 
    1L), .Dimnames = list("expr", NULL))))

I want to convert my_list to a DataFrame where the rows are the element names (ENSGs) as ID and columns are the correspong r coeffiecients. Then, calculate the mean for each row to be restored in a new column mean. Also, compute the r.squared of column mean and restore it in a new column as r2.

I actually tried in this way that I stopped with this error:

my_list_df <- as.data.frame(do.call(rbind, my_list))
my_list_df$ID <- rownames(my_list_df)
rownames(my_list_df) <- NULL
my_list_df <- my_list_df[,c(6,1,2,3,4,5)]

my_list_df$mean <- rowMeans(my_list_df)

Error in base::rowMeans(x, na.rm = na.rm, dims = dims, ...) : 
  'x' must be numeric

whould you help please!

UPDATE:

Following the @akrun's solution, here is the script to complete the task.

my_list_df <- as.data.frame(do.call(rbind, my_list))
my_list_df$ID <- rownames(my_list_df)
rownames(my_list_df) <- NULL
my_list_df <- my_list_df[,c(6,1,2,3,4,5)]

my_list_df[-1] <- unlist(my_list_df[-1])
my_list_df$mean <- rowMeans(my_list_df[-1])

## square the Mean column to get the r.squared values. 
options(scipen = 999)
my_list_df$r2 <- my_list_df[, "mean"]^2

CodePudding user response:

The reason is that the my_list_df is having list columns. We may have to change it by unlisting and assingning back the numeric columns to those and then do the rowMeans on the numeric columns

my_list_df[-1] <- unlist(my_list_df[-1])
rowMeans(my_list_df[-1])
[1]  0.148957632 -0.004624657  0.077046759 -0.039692762  0.073785343

Or using map

library(purrr)
colMeans(map_dfc(my_list, unlist))
ENSG00000141956 ENSG00000141959 ENSG00000142149 ENSG00000142156 ENSG00000142166 
    0.148957632    -0.004624657     0.077046759    -0.039692762     0.073785343 
  • Related