gpt4 book ai didi

r - 从矩阵列表中获取元素

转载 作者:行者123 更新时间:2023-12-02 06:19:23 24 4
gpt4 key购买 nike

假设我有一小部分矩阵,想提取每个矩阵的加入。除了循环 attr() 函数之外,还有什么好的方法可以做到这一点。

矩阵数据:

tfmatrx<-list(

MA0275.1 = structure(c(0, 76, 0, 24, 0, 100, 0, 0, 0, 0,
100, 0, 0, 0, 100, 0, 72, 11, 16, 0, 53, 0, 0, 47), .Dim = c(4L,
6L), .Dimnames = list(c("A", "C", "G", "T"), NULL), id = "MA0275.1", accession = "ASG1"),
MA0276.1 = structure(c(0, 220, 8, 35, 0, 291, 0, 3, 61, 21,
133, 10, 58, 54, 101, 12, 130, 0, 54, 0, 0, 11, 8, 147, 33,
150, 8, 35, 80, 0, 92, 26, 0, 8, 249, 19, 0, 0, 256, 18), .Dim = c(4L,
10L), .Dimnames = list(c("A", "C", "G", "T"), NULL), id = "MA0276.1", accession = "ASH1"),
MA0277.1 = structure(c(63, 13, 13, 13, 100, 0, 0, 0, 100,
0, 0, 0, 88, 13, 0, 0, 75, 0, 25, 0, 0, 0, 100, 0, 78, 16,
3, 3, 81, 6, 6, 6, 63, 13, 13, 13), .Dim = c(4L, 9L), .Dimnames = list(
c("A", "C", "G", "T"), NULL), id = "MA0277.1", accession = "AZF1"),
MA0278.1 = structure(c(64, 217, 425, 292, 104, 552, 150,
192, 484, 111, 114, 288, 78, 401, 186, 333, 455, 51, 370,
122, 248, 34, 670, 46, 98, 724, 143, 33, 52, 918, 7, 20,
348, 346, 280, 24, 12, 3, 977, 6, 966, 5, 23, 4, 26, 6, 962,
4, 9, 10, 4, 975, 47, 930, 7, 15, 892, 42, 16, 49, 487, 123,
320, 68, 288, 140, 317, 254, 373, 110, 81, 434, 178, 367,
184, 268, 402, 140, 341, 114, 435, 229, 241, 94), .Dim = c(4L,
21L), .Dimnames = list(c("A", "C", "G", "T"), NULL), id = "MA0278.1", accession = "BAS1"))

我想获取字符向量或 data.frame 中的所有种质。我知道如何为列表中的第一个元素执行此操作,但如何为所有加入项执行此操作?

我想在这里实现的输出是:

ASG1
ASH1
ASF1
BAS1

names(tfmatrix) 给出了所有的名字,但没有提供任何帮助。

最佳答案

怎么样

lapply(tfmatrx, attr, "accession")
$MA0275.1
[1] "ASG1"

$MA0276.1
[1] "ASH1"

$MA0277.1
[1] "AZF1"

$MA0278.1
[1] "BAS1"

您还可以使用 sapply,这很方便,因为输出是一个向量(sapply 会尽可能地简化结果)。

sapply(tfmatrx, attr, "accession")
MA0275.1 MA0276.1 MA0277.1 MA0278.1
"ASG1" "ASH1" "AZF1" "BAS1"

这等同于上面未列出的lapply解决方案。

unlist(lapply(tfmatrx, attr, "accession"))
MA0275.1 MA0276.1 MA0277.1 MA0278.1
"ASG1" "ASH1" "AZF1" "BAS1"

关于r - 从矩阵列表中获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15244608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com