我有一個元素串列,包括每個基因的五個 r 系數:
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))))
我想轉換my_list為 DataFrame,其中行是元素名稱(ENSG)ID,列是對應的 r 系數。然后,計算要在新列中恢復的每一行的平均值mean。此外,計算列的 r.squaredmean并將其在新列中恢復為r2.
我實際上以這種方式嘗試過,但由于此錯誤而停止了:
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
請你幫忙!
更新:
按照@akrun 的解決方案,這里是完成任務的腳本。
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
uj5u.com熱心網友回復:
原因是my_list_df有list列。我們可能必須通過unlist將數字列重新分配給那些來更改它,然后rowMeans在數字列上執行
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
或使用map
library(purrr)
colMeans(map_dfc(my_list, unlist))
ENSG00000141956 ENSG00000141959 ENSG00000142149 ENSG00000142156 ENSG00000142166
0.148957632 -0.004624657 0.077046759 -0.039692762 0.073785343
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/441615.html
