列表中所有数据框中的R gsub列名称 [英] R gsub column names in all data frames within a list
本文介绍了列表中所有数据框中的R gsub列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有列名的数据帧列表,我想替换掉一些字符串,但是我做不到.
I have a list of data frames with column names that I want to substitute some strings out, but I can't do it right.
list:
[1]
cpg value.TCGA.06.5415.01A value.TCGA.02.0003.01A value.TCGA.16.1062.01A
cg02726808 0.934641544 NA NA
cg04243127 0.8828403 NA NA
[2]
cpg value.TCGA.QH.A6CV.01A value.TCGA.E1.A7Z4.01A value.TCGA.E1.5303.01A
cg02726808 0.938556343 0.92163563 0.959269597
cg04243127 0.886928811 0.842963126 0.937700666
[N]
.....
Desired output:
list:
[1]
cpg 06.5415.01A 02.0003.01A 16.1062.01A
cg02726808 0.934641544 NA NA
cg04243127 0.8828403 NA NA
[2]
cpg QH.A6CV.01A E1.A7Z4.01A E1.5303.01A
cg02726808 0.938556343 0.92163563 0.959269597
cg04243127 0.886928811 0.842963126 0.937700666
[N]
.....
我尝试编写以下内容:
lapply(lst, function(x) { gsub("value.TCGA.", "", colnames(lst[[x]]))})
R Studio中的错误:
Error in R Studio:
Error in llis1[[xy]] : invalid subscript type 'list'
Called from: is.data.frame(x)
Browse[1]>
我不明白这是怎么回事.感谢您的帮助.
And I don't understand what is it about. Thanks for your help.
推荐答案
我们可以使用 setNames
将列名替换为 sub
中的新列名./p>
We can use setNames
to replace the column names with the new column names from the sub
.
lapply(lst, function(x) setNames(x, sub("value.TCGA.", "", names(x))))
如果'value.TCGA',则另一个选项是 substring
.除第一个列名称外,所有列名称的位置均相同.
Or another option is substring
if the 'value.TCGA.' position is the same in all the column names except the first one.
lapply(lst, function(x) setNames(x, c("cpg", substring(names(x)[-1], 12)))
这篇关于列表中所有数据框中的R gsub列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文