r - 获取相关结果后如何更改列表 [[]] 名称(r 和 p)
问题描述
我有一个数据,我使用 lapply 循环计算了数据框中一个基因与另一个基因之间的相关性。
现在总结果存储在一个列表中。下一步我想通过基因符号提取一个结果,就像这样: Correlation.list[["gene symbol"]]
但结果只显示使用 number:Correlation.list[[1]]
这是我的示例数据和代码:
data_logg<-structure(c(6.05572382866802, 11.1380021588504, 9.3044407551291,
7.87123980178745, 10.1452025129037, 8.93954331139168, 7.72897302870656,
8.31753461010792, 6.91902649139208, 8.81063297295094, 22.5569750353369,
31.520979452157, 28.3261317078564, 25.402957920785, 35.8148569235307,
27.8723220522029, 41.0335341398849, 28.5846501726903, 21.398001509988,
33.063696558847, 15.182913110301, 14.6438943008441, 16.1624032499377,
13.1264245066984, 13.4072656803608, 14.7364553246895, 13.1211732101273,
14.3003714459557, 14.918175412959, 15.7912093225492, 0.0931714621767618,
0.0303852980725358, 0.0114778232990823, 0.0260809645231031, 0,
0.0310539968593767, 0.019047166325137, 0.0105050244811974, 0.0264828042698263,
0.0346757324524723, 3.46286706915552, 4.99156437489882, 5.70180521646014,
4.0868441874337, 4.51377652615602, 5.35554484236395, 5.44397291505049,
6.57811217176637, 5.10097757787774, 5.18489532380933, 1.12546006270081,
1.91823256736007, 1.8500381393557, 1.4401998592, 1.14712309386819,
1.63756861783462, 1.63809356500207, 1.99896249233356, 1.3388769544766,
2.07437306868356, 1.5068638533804, 2.63183788279904, 3.12822707867838,
2.44752756389731, 2.37001697139819, 2.51118444838866, 3.48267851492631,
3.26267014874084, 1.75288566197561, 2.80059464803222, 21.3209507790769,
22.6744418461091, 16.9622647095367, 22.2902884855603, 25.7854403101755,
20.6976499521803, 24.1019869113154, 24.764924561036, 22.8547950562338,
15.9953039663019), .Dim = c(10L, 8L), .Dimnames = list(NULL,
c("Zzef1", "Zyx", "Zyg11b", "Zyg11a", "Zxdc", "Zxdb", "Zxda",
"Zwint")))
代码:
Correlation_list<-lapply(colnames(data_logg),function(ii){
i<-match(ii,colnames(data_logg))
# i<-ii %in% colnames(data_logg)
tm <- corr.test(data_logg[,i,drop=FALSE],
y = data_logg[,-i], use = "pairwise", "spearman", adjust="none",
alpha=0.05, ci=F, minlength=5)
res<-t(do.call(rbind, tm[c("r", "p")]))
# res$r<-signif(res$r,3) ## 3位小数点后
# res$p<-signif(res$p,3) ## 3位有效数字
colnames(res)<- c(paste0(ii,"_Correlaiton"), paste0(ii,"_P_value"))
na.omit(res)
})
##################
Correlation_list[[1]] ## it's ok
Correlation_list[["Zzz3"]] ## doesn't work
所以我的问题是如何通过 Correlation_list[["Zzz3"]] 提取我需要的数据
感激不尽。
解决方案
Correlation_list
没有名称,因此您将无法使用Correlation.list[["gene symbol"]]
.
这里有两个选择——
- 将名称分配给
Correlation_list
afterlapply
循环。
names(Correlation_list) <- colnames(data_logg)
- 更改
lapply
为sapply
which 将自动给出名称。
Correlation_list<- sapply(colnames(data_logg),function(ii){
i<-match(ii,colnames(data_logg))
# i<-ii %in% colnames(data_logg)
tm <- psych::corr.test(data_logg[,i,drop=FALSE],
y = data_logg[,-i], use = "pairwise", "spearman", adjust="none",
alpha=0.05, ci=F, minlength=5)
res<-t(do.call(rbind, tm[c("r", "p")]))
# res$r<-signif(res$r,3) ## 3位小数点后
# res$p<-signif(res$p,3) ## 3位有效数字
colnames(res)<- c(paste0(ii,"_Correlaiton"), paste0(ii,"_P_value"))
na.omit(res)
}, simplify = FALSE)
然后,您可以使用基因名称来提取相关数据。
Correlation_list[["Zwint"]]
#Same as
#Correlation_list$Zwint
# Zwint_Correlaiton Zwint_P_value
#Zzef1 -0.030303030 0.93377296
#Zyx 0.284848485 0.42503815
#Zyg11b -0.733333333 0.01580060
#Zyg11a -0.660606061 0.03758838
#Zxdc -0.006060606 0.98674291
#Zxdb -0.260606061 0.46708905
#Zxda 0.006060606 0.98674291
推荐阅读
- r - 使用 R Separate_Rows 不适用于“|”
- c# - 在同一 Visual Studio 表单中而不是在新窗口中打开链接 (C#)
- r - 在 R 中为分类/连续变量操作 ggplor2
- python - 推送拒绝了 Heroku Python Flask 要求失败 exiv2==0.3.1
- computer-vision - HALCON min_mas_grey()
- swift - 带有“App Scene WindowGroup”的白色状态栏
- javascript - 按下提交后如何记录引导下拉菜单的值?
- python - 从 Plex 的“最近添加”部分中删除视频
- docker - 在 Docker 中将 Node-Red 连接到 Mariadb
- machine-learning - 如何使用 PyTorch 计算 Monte Carlo Dropout 神经网络的不确定性?