数据表列表的唯一列名

时间:2015-05-05 17:47:57

标签: r list merge dataframe data.table

我有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

1 个答案:

答案 0 :(得分:3)

如果' 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