R语言 从子列表的名称中删除字符串

jogvjijk  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(158)

我有一个列表,我需要删除"基因-"无论它发生在哪里。我试过

lapply(net, FUN = function(x) setNames(x, sub("gene-","", x)))

但我得到了错误
名称错误(对象)〈-nm:尝试将属性设置为NULL

head(net)
$colors
        gene-AAAS         gene-AAK1        gene-AAMDC         gene-AAMP        gene-AARS1        gene-AASDH 
        "magenta"           "brown"          "purple"        "darkgrey"           "brown"            "blue" 
    gene-AASDHPPT         gene-AASS         gene-AATK         gene-ABAT        
 [ reached getOption("max.print") -- omitted 8990 entries ]

$unmergedColors
        gene-AAAS         gene-AAK1        gene-AAMDC         gene-AAMP        gene-AARS1        gene-AASDH 
       "darkgrey"            "blue"         "magenta"  "darkolivegreen"            "blue"           "brown" 
    gene-AASDHPPT         gene-AASS         gene-AATK         gene-ABAT        gene-ABCA1       gene-ABCA12 
    "lightyellow"      "lightgreen"       "turquoise"         "darkred"       "turquoise"          "grey60" 

 [ reached getOption("max.print") -- omitted 8990 entries ]

$MEs
        MEblack MEgreenyellow MElightcyan    MEyellow MEturquoise      MEpink     MEwhite   MEdarkred
M5  -0.17423916   0.141440817  0.23401244  0.36358728  -0.0220835 -0.18126013  0.05942248 -0.45035371
N3   0.47690393   0.428961135  0.07241255 -0.02557197   0.2238352  0.06742087 -0.09574663  0.52201599

          
$goodSamples
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

$goodGenes
   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 [ reached getOption("max.print") -- omitted 8990 entries ]

$dendrograms
$dendrograms[[1]]

Call:
fastcluster::hclust(d = as.dist(dissTom), method = "average")

Cluster method   : average 
Number of objects: 9990

产出(净额)

166L, 5768L, 2346L, 7132L, 625L, 4848L, 736L, 7001L, 
    1721L, 6626L, 7674L, 2543L, 7013L, 8667L, 4593L, 2804L,

....

7435L, 4895L, 8462L, 1732L, 3160L, 8529L), labels = NULL, 
        method = "average", call = fastcluster::hclust(d = as.dist(dissTom), 
            method = "average"), dist.method = NULL), class = "hclust")), 
    TOMFiles = NULL, blockGenes = list(1:9990), blocks = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

...

1, 1), MEsOK = TRUE)
zfycwa2u

zfycwa2u1#

您的代码几乎可以正常工作,但您需要做两个更改:
1.我们想修改 names,所以内部函数需要读取names(x)

function(x) setNames(x, sub("gene-","", names(x)))
  1. net包含很多成员,你只想替换colorsunmergedColors成员,所以只对它们应用你的函数,哦,你需要把结果赋回你的对象:
which = c("colors", "unmergedColors")
net[which] = lapply(net[which], function(x) setNames(x, sub("gene-","", names(x))))

相关问题