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

huangapple go评论63阅读模式
英文:

Delete string from names in sublists

问题

我需要删除所有出现 "gene-" 的地方。

我尝试了以下代码:

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

但是我得到了错误:

Error in names(object) <- nm : attempt to set an attribute on 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

dput(net)

    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)
英文:

I have a list of lists I need to delete "gene-" everywhere where it happens.
I tried

 lapply(net, FUN = function(x) setNames(x, sub(&quot;gene-&quot;,&quot;&quot;, x)))

but I get the error

>Error in names(object) <- nm : attempt to set an attribute on NULL

 head(net)
$colors
gene-AAAS         gene-AAK1        gene-AAMDC         gene-AAMP        gene-AARS1        gene-AASDH 
&quot;magenta&quot;           &quot;brown&quot;          &quot;purple&quot;        &quot;darkgrey&quot;           &quot;brown&quot;            &quot;blue&quot; 
gene-AASDHPPT         gene-AASS         gene-AATK         gene-ABAT        
[ reached getOption(&quot;max.print&quot;) -- omitted 8990 entries ]
$unmergedColors
gene-AAAS         gene-AAK1        gene-AAMDC         gene-AAMP        gene-AARS1        gene-AASDH 
&quot;darkgrey&quot;            &quot;blue&quot;         &quot;magenta&quot;  &quot;darkolivegreen&quot;            &quot;blue&quot;           &quot;brown&quot; 
gene-AASDHPPT         gene-AASS         gene-AATK         gene-ABAT        gene-ABCA1       gene-ABCA12 
&quot;lightyellow&quot;      &quot;lightgreen&quot;       &quot;turquoise&quot;         &quot;darkred&quot;       &quot;turquoise&quot;          &quot;grey60&quot; 
[ reached getOption(&quot;max.print&quot;) -- 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(&quot;max.print&quot;) -- omitted 8990 entries ]
$dendrograms
$dendrograms[[1]]
Call:
fastcluster::hclust(d = as.dist(dissTom), method = &quot;average&quot;)
Cluster method   : average 
Number of objects: 9990 

dput(net)

    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 = &quot;average&quot;, call = fastcluster::hclust(d = as.dist(dissTom), 
method = &quot;average&quot;), dist.method = NULL), class = &quot;hclust&quot;)), 
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)

答案1

得分: 1

  1. 您需要修改names,因此您的内部函数需要读取names(x)

    function(x) setNames(x, sub("gene-", "", names(x)))
    
  2. net 包含许多成员。您只想替换colorsunmergedColors成员,因此只将您的函数应用于这些成员。噢,您还需要将结果分配回您的对象:

    which = c("colors", "unmergedColors")
    net[which] = lapply(net[which], function(x) setNames(x, sub("gene-", "", names(x)))
    
英文:

Your code almost works, you need two changes:

  1. You want to modify the names, so your inner function needs to read names(x):

    function(x) setNames(x, sub(&quot;gene-&quot;, &quot;&quot;, names(x)))
    
  2. net contains a lot of members. You only want to replace the colors and unmergedColors members, so apply your function to only those. Oh, and you need to assign the result back to your object:

    which = c(&quot;colors&quot;, &quot;unmergedColors&quot;)
    net[which] = lapply(net[which], function(x) setNames(x, sub(&quot;gene-&quot;, &quot;&quot;, names(x))))
    

huangapple
  • 本文由 发表于 2023年2月13日 23:23:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定