根据另一个列表中的文本重命名数据框列

时间:2020-10-09 12:49:55

标签: r

我有一些看起来像这样的数据

[[1]]
[1] ""     "2019" "2020" "2021" "2022" "2023"

[[2]]
[1] ""     "2019" "2020" "2021" "2022" "2023"

[[3]]
[1] ""     "2020" "2021" "2022" "2023"

[[1]]
                X1       X2        X3        X4        X5        X6
1   Ventas Cliente 98716127   1477902   1970536   2955805   4433708
2       Revenue\n€  987,161 1,477,902 1,970,536 2,955,805 4,433,708
3   Expenditure\n€  890,623 1,337,031 1,732,873 2,569,122 3,829,434
4 Profit (Loss)\n€   96,538   140,871   237,663   386,683   604,274

[[2]]
                            X1 X2   X3     X4       X5        X6
1 Number of registers that pay  0    5    200     2000     10000
2                   Revenue\n€  0    0 13,000  300,000 1,700,000
3               Expenditure\n€  0  500 22,000  500,000 2,000,000
4             Profit (Loss)\n€  0 -500 -9,000 -200,000  -300,000

[[3]]
                             X1     X2       X3        X4        X5
1 Ventas espacios publicitarios     --       --        --        --
2                    Revenue\n€  2,000  150,000 1,200,000 5,000,000
3                Expenditure\n€  5,500  250,000   600,000 1,500,000
4              Profit (Loss)\n€ -3,500 -100,000   600,000 3,500,000

我想通过列表myNames重命名每个列表数据框的列。

预期的输出(对于第一个列表项):

             ""       "2019"   "2020"    "2021"    "2022"    "2023"
1   Ventas Cliente 98716127   1477902   1970536   2955805   4433708
2       Revenue\n€  987,161 1,477,902 1,970,536 2,955,805 4,433,708
3   Expenditure\n€  890,623 1,337,031 1,732,873 2,569,122 3,829,434
4 Profit (Loss)\n€   96,538   140,871   237,663   386,683   604,274

此数据框列表现在具有myNames中相应的列名。

数据:

列名:

myNames <- list(c("", "2019", "2020", "2021", "2022", "2023"), c("", "2019", 
"2020", "2021", "2022", "2023"), c("", "2020", "2021", "2022", 
"2023"), c("", "2021", "2022", "2023"), c("", "2017", "2018", 
"2019", "2020", "2021", "2022", "2023"), c("", "2019", "2020", 
"2021", "2022", "2023"))

表格:

myTables <- list(structure(list(X1 = c("Ventas Cliente", "Revenue\n\200", 
"Expenditure\n\200", "Profit (Loss)\n\200"), X2 = c("98716127", 
"987,161", "890,623", "96,538"), X3 = c("1477902", "1,477,902", 
"1,337,031", "140,871"), X4 = c("1970536", "1,970,536", "1,732,873", 
"237,663"), X5 = c("2955805", "2,955,805", "2,569,122", "386,683"
), X6 = c("4433708", "4,433,708", "3,829,434", "604,274")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(X1 = c("Number of registers that pay", 
"Revenue\n\200", "Expenditure\n\200", "Profit (Loss)\n\200"), 
    X2 = c(0L, 0L, 0L, 0L), X3 = c(5L, 0L, 500L, -500L), X4 = c("200", 
    "13,000", "22,000", "-9,000"), X5 = c("2000", "300,000", 
    "500,000", "-200,000"), X6 = c("10000", "1,700,000", "2,000,000", 
    "-300,000")), class = "data.frame", row.names = c(NA, -4L
)), structure(list(X1 = c("Ventas espacios publicitarios", "Revenue\n\200", 
"Expenditure\n\200", "Profit (Loss)\n\200"), X2 = c("--", "2,000", 
"5,500", "-3,500"), X3 = c("--", "150,000", "250,000", "-100,000"
), X4 = c("--", "1,200,000", "600,000", "600,000"), X5 = c("--", 
"5,000,000", "1,500,000", "3,500,000")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(X1 = c("Acuerdos I+D, licencias y royalties", 
"Revenue\n\200", "Expenditure\n\200", "Profit (Loss)\n\200"), 
    X2 = c("--", "720,013", "711,153", "8,860"), X3 = c("--", 
    "3,897,696", "1,043,255", "2,854,441"), X4 = c("--", "8,752,244", 
    "1,063,784", "7,688,460")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(X1 = c("0", "Revenue\n$", "Expenditure\n$", 
"Profit (Loss)\n$"), X2 = c(0L, 0L, 0L, 0L), X3 = c(0L, 0L, 0L, 
0L), X4 = c(0L, 0L, 0L, 0L), X5 = c("1", "116,288", "73,920", 
"42,368"), X6 = c("2", "1,153,119", "96,096", "1,057,023"), X7 = c("3", 
"10,397,610", "124,925", "10,272,685"), X8 = c("4", "92,822,419", 
"162,403", "92,660,016")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(X1 = c("end-to-end integration", "Revenue\n\200", 
"Expenditure\n\200", "Profit (Loss)\n\200"), X2 = c(0L, 0L, 0L, 
0L), X3 = c("8", "20,000", "37,000", "-17,000"), X4 = c("22", 
"431,000", "451,000", "-20,000"), X5 = c("37", "927,000", "523,000", 
"404,000"), X6 = c("52", "1,386,000", "630,000", "756,000")), class = "data.frame", row.names = c(NA, 
-4L)))

4 个答案:

答案 0 :(得分:3)

您可以在setNames内尝试Map

Map(function(x, y) setNames(x,y), myTables, myNames)

或更简单的一个(感谢@jogo)

Map(setNames, myTables, myNames)

答案 1 :(得分:2)

您可以使用Map

Map(function(x, y) {names(x) <- y;x}, myTables, myNames)

与purrr的map2类似:

purrr::map2(myTables, myNames, ~{names(.x) <- .y;.x})

答案 2 :(得分:2)

您还可以使用:

#Code
New <- mapply(function(x,y) setNames(x,y),myTables,myNames)

输出:

[[1]]
                       2019      2020      2021      2022      2023
1   Ventas Cliente 98716127   1477902   1970536   2955805   4433708
2       Revenue\n€  987,161 1,477,902 1,970,536 2,955,805 4,433,708
3   Expenditure\n€  890,623 1,337,031 1,732,873 2,569,122 3,829,434
4 Profit (Loss)\n€   96,538   140,871   237,663   386,683   604,274

[[2]]
                               2019 2020   2021     2022      2023
1 Number of registers that pay    0    5    200     2000     10000
2                   Revenue\n€    0    0 13,000  300,000 1,700,000
3               Expenditure\n€    0  500 22,000  500,000 2,000,000
4             Profit (Loss)\n€    0 -500 -9,000 -200,000  -300,000

[[3]]
                                  2020     2021      2022      2023
1 Ventas espacios publicitarios     --       --        --        --
2                    Revenue\n€  2,000  150,000 1,200,000 5,000,000
3                Expenditure\n€  5,500  250,000   600,000 1,500,000
4              Profit (Loss)\n€ -3,500 -100,000   600,000 3,500,000

[[4]]
                                         2021      2022      2023
1 Acuerdos I+D, licencias y royalties      --        --        --
2                          Revenue\n€ 720,013 3,897,696 8,752,244
3                      Expenditure\n€ 711,153 1,043,255 1,063,784
4                    Profit (Loss)\n€   8,860 2,854,441 7,688,460

[[5]]
                   2017 2018 2019    2020      2021       2022       2023
1                0    0    0    0       1         2          3          4
2       Revenue\n$    0    0    0 116,288 1,153,119 10,397,610 92,822,419
3   Expenditure\n$    0    0    0  73,920    96,096    124,925    162,403
4 Profit (Loss)\n$    0    0    0  42,368 1,057,023 10,272,685 92,660,016

[[6]]
                         2019    2020    2021    2022      2023
1 end-to-end integration    0       8      22      37        52
2             Revenue\n€    0  20,000 431,000 927,000 1,386,000
3         Expenditure\n€    0  37,000 451,000 523,000   630,000
4       Profit (Loss)\n€    0 -17,000 -20,000 404,000   756,000

答案 3 :(得分:2)

我们可以使用lapply

lapply(seq_along(myTables), function(i) setNames(myTables[[i]], myNames[[i]]))
相关问题