英文:
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("gene-","", 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 
"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)
答案1
得分: 1
- 
您需要修改names,因此您的内部函数需要读取
names(x):function(x) setNames(x, sub("gene-", "", names(x))) - 
net包含许多成员。您只想替换colors和unmergedColors成员,因此只将您的函数应用于这些成员。噢,您还需要将结果分配回您的对象: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:
- 
You want to modify the names, so your inner function needs to read
names(x):function(x) setNames(x, sub("gene-", "", names(x))) - 
netcontains a lot of members. You only want to replace thecolorsandunmergedColorsmembers, so apply your function to only those. Oh, and you need to assign the result back to your object:which = c("colors", "unmergedColors") net[which] = lapply(net[which], function(x) setNames(x, sub("gene-", "", names(x)))) 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论