gpt4 book ai didi

r - 数据表列表的唯一列名

转载 作者:行者123 更新时间:2023-12-01 23:43:57 24 4
gpt4 key购买 nike

我有一个包含 200 个数据表的列表,如下所示

 [[1]]
SYMBOL SIGNALINTENSITY CALL PVALUE
1: DDR1 1272.0796 P 0.0029235941
2: MIR4640 1272.0796 P 0.0029235941
3: RFC2 501.8763 A 0.2040220548
4: HSPA6 721.1877 P 0.0038229257
5: PAX8 2807.3700 P 0.0006731656
6: GUCA1A 213.1593 A 0.2668473097

[[2]]
SYMBOL SIGNALINTENSITY CALL PVALUE
1: FAM86B1 168.5479 A 0.2189832287
2: FAM86FP 168.5479 A 0.2189832287
3: FAM86B2 168.5479 A 0.2189832287
4: FAM86DP 168.5479 A 0.2189832287
5: LOC100505915 773.4419 P 0.0005617505
6: LINC00273 670.4535 P 0.0002967575
[[3]]
SYMBOL SIGNALINTENSITY CALL PVALUE
1: FAM86B1 168.5479 A 0.2189832287
2: FAM86FP 168.5479 A 0.2189832287
3: FAM86B2 168.5479 A 0.2189832287
4: FAM86DP 168.5479 A 0.2189832287
5: LOC100505915 773.4419 P 0.0005617505
6: LINC00273 670.4535 P 0.0002967575

我想唯一地重命名数据表列。如下所示,根据列表顺序,列带有 1,2,3 的下标。第一列名称应该相同,因为我需要按第一列合并列表。
 [[1]]
SYMBOL SIGNALINTENSITY1 CALL1 PVALUE1
1: DDR1 1272.0796 P 0.0029235941
2: MIR4640 1272.0796 P 0.0029235941
3: RFC2 501.8763 A 0.2040220548
4: HSPA6 721.1877 P 0.0038229257
5: PAX8 2807.3700 P 0.0006731656
6: GUCA1A 213.1593 A 0.2668473097

[[2]]
SYMBOL SIGNALINTENSITY2 CALL2 PVALUE2
1: FAM86B1 168.5479 A 0.2189832287
2: FAM86FP 168.5479 A 0.2189832287
3: FAM86B2 168.5479 A 0.2189832287
4: FAM86DP 168.5479 A 0.2189832287
5: LOC100505915 773.4419 P 0.0005617505
6: LINC00273 670.4535 P 0.0002967575
[[3]]
SYMBOL SIGNALINTENSITY3 CALL3 PVALUE3
1: FAM86B1 168.5479 A 0.2189832287
2: FAM86FP 168.5479 A 0.2189832287
3: FAM86B2 168.5479 A 0.2189832287
4: FAM86DP 168.5479 A 0.2189832287
5: LOC100505915 773.4419 P 0.0005617505
6: LINC00273 670.4535 P 0.0002967575

最佳答案

如果 'lst' 是 data.table 的列表(似乎 data.table 而不是 data.frame )

 library(data.table)
lapply(seq_along(lst), function(i) setnames(lst[[i]],
2:ncol(lst[[i]]), paste0(names(lst[[i]])[-1],i)))

lst
#[[1]]
# SYMBOL SIGNALINTENSITY1 CALL1 PVALUE1
#1: DDR1 1272.0796 P 0.0029235941
#2: MIR4640 1272.0796 P 0.0029235941
#3: RFC2 501.8763 A 0.2040220548
#4: HSPA6 721.1877 P 0.0038229257
#5: PAX8 2807.3700 P 0.0006731656
#6: GUCA1A 213.1593 A 0.2668473097

#[[2]]
# SYMBOL SIGNALINTENSITY2 CALL2 PVALUE2
#1: FAM86B1 168.5479 A 0.2189832287
#2: FAM86FP 168.5479 A 0.2189832287
#3: FAM86B2 168.5479 A 0.2189832287
#4: FAM86DP 168.5479 A 0.2189832287
#5: LOC100505915 773.4419 P 0.0005617505
#6: LINC00273 670.4535 P 0.0002967575

#[[3]]
# SYMBOL SIGNALINTENSITY3 CALL3 PVALUE3
#1: FAM86B1 168.5479 A 0.2189832287
#2: FAM86FP 168.5479 A 0.2189832287
#3: FAM86B2 168.5479 A 0.2189832287
#4: FAM86DP 168.5479 A 0.2189832287
#5: LOC100505915 773.4419 P 0.0005617505
#6: LINC00273 670.4535 P 0.0002967575

关于r - 数据表列表的唯一列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30059853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com