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

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

Delete string from names in sublists

问题

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

我尝试了以下代码:

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

但是我得到了错误:

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

以下是要翻译的代码和输出:

  1. head(net)
  2. $colors
  3. gene-AAAS gene-AAK1 gene-AAMDC gene-AAMP gene-AARS1 gene-AASDH
  4. "magenta" "brown" "purple" "darkgrey" "brown" "blue"
  5. gene-AASDHPPT gene-AASS gene-AATK gene-ABAT
  6. [ reached getOption("max.print") -- omitted 8990 entries ]
  7. $unmergedColors
  8. gene-AAAS gene-AAK1 gene-AAMDC gene-AAMP gene-AARS1 gene-AASDH
  9. "darkgrey" "blue" "magenta" "darkolivegreen" "blue" "brown"
  10. gene-AASDHPPT gene-AASS gene-AATK gene-ABAT gene-ABCA1 gene-ABCA12
  11. "lightyellow" "lightgreen" "turquoise" "darkred" "turquoise" "grey60"
  12. [ reached getOption("max.print") -- omitted 8990 entries ]
  13. $MEs
  14. MEblack MEgreenyellow MElightcyan MEyellow MEturquoise MEpink MEwhite MEdarkred
  15. M5 -0.17423916 0.141440817 0.23401244 0.36358728 -0.0220835 -0.18126013 0.05942248 -0.45035371
  16. N3 0.47690393 0.428961135 0.07241255 -0.02557197 0.2238352 0.06742087 -0.09574663 0.52201599
  17. $goodSamples
  18. [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
  19. $goodGenes
  20. [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
  21. [ reached getOption("max.print") -- omitted 8990 entries ]
  22. $dendrograms
  23. $dendrograms[[1]]
  24. Call:
  25. fastcluster::hclust(d = as.dist(dissTom), method = "average")
  26. Cluster method : average
  27. Number of objects: 9990
  28. dput(net)
  29. 166L, 5768L, 2346L, 7132L, 625L, 4848L, 736L, 7001L,
  30. 1721L, 6626L, 7674L, 2543L, 7013L, 8667L, 4593L, 2804L,
  31. ....
  32. 7435L, 4895L, 8462L, 1732L, 3160L, 8529L), labels = NULL,
  33. method = "average", call = fastcluster::hclust(d = as.dist(dissTom),
  34. method = "average"), dist.method = NULL), class = "hclust")),
  35. TOMFiles = NULL, blockGenes = list(1:9990), blocks = c(1,
  36. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. ...
  39. 1, 1), MEsOK = TRUE)
英文:

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

  1. 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

  1. head(net)
  2. $colors
  3. gene-AAAS gene-AAK1 gene-AAMDC gene-AAMP gene-AARS1 gene-AASDH
  4. &quot;magenta&quot; &quot;brown&quot; &quot;purple&quot; &quot;darkgrey&quot; &quot;brown&quot; &quot;blue&quot;
  5. gene-AASDHPPT gene-AASS gene-AATK gene-ABAT
  6. [ reached getOption(&quot;max.print&quot;) -- omitted 8990 entries ]
  7. $unmergedColors
  8. gene-AAAS gene-AAK1 gene-AAMDC gene-AAMP gene-AARS1 gene-AASDH
  9. &quot;darkgrey&quot; &quot;blue&quot; &quot;magenta&quot; &quot;darkolivegreen&quot; &quot;blue&quot; &quot;brown&quot;
  10. gene-AASDHPPT gene-AASS gene-AATK gene-ABAT gene-ABCA1 gene-ABCA12
  11. &quot;lightyellow&quot; &quot;lightgreen&quot; &quot;turquoise&quot; &quot;darkred&quot; &quot;turquoise&quot; &quot;grey60&quot;
  12. [ reached getOption(&quot;max.print&quot;) -- omitted 8990 entries ]
  13. $MEs
  14. MEblack MEgreenyellow MElightcyan MEyellow MEturquoise MEpink MEwhite MEdarkred
  15. M5 -0.17423916 0.141440817 0.23401244 0.36358728 -0.0220835 -0.18126013 0.05942248 -0.45035371
  16. N3 0.47690393 0.428961135 0.07241255 -0.02557197 0.2238352 0.06742087 -0.09574663 0.52201599
  17. $goodSamples
  18. [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
  19. $goodGenes
  20. [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
  21. [ reached getOption(&quot;max.print&quot;) -- omitted 8990 entries ]
  22. $dendrograms
  23. $dendrograms[[1]]
  24. Call:
  25. fastcluster::hclust(d = as.dist(dissTom), method = &quot;average&quot;)
  26. Cluster method : average
  27. Number of objects: 9990

dput(net)

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

....

  1. 7435L, 4895L, 8462L, 1732L, 3160L, 8529L), labels = NULL,
  2. method = &quot;average&quot;, call = fastcluster::hclust(d = as.dist(dissTom),
  3. method = &quot;average&quot;), dist.method = NULL), class = &quot;hclust&quot;)),
  4. TOMFiles = NULL, blockGenes = list(1:9990), blocks = c(1,
  5. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6. 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)

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

    1. which = c("colors", "unmergedColors")
    2. 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):

    1. 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:

    1. which = c(&quot;colors&quot;, &quot;unmergedColors&quot;)
    2. 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:

确定