【问题标题】:How to Make Column Names an actual column in a data frame如何使列名成为数据框中的实际列
【发布时间】:2023-04-02 22:38:02
【问题描述】:

我正在使用的数据的一个小样本如下:

structure(list(`Team Code` = c(107, 277, 129, 1320, 27, 694, 
231, 306, 314, 465), `Opponent Code` = c(277, 107, 1320, 129, 
694, 27, 306, 231, 465, 314), Year = c(2016, 2016, 2016, 2016, 
2016, 2016, 2016, 2016, 2016, 2016), Date = structure(c(17040, 
17040, 17045, 17045, 17045, 17045, 17045, 17045, 17045, 17045
), class = "Date"), HomeTeam = c(0, 0, 1, 0, 0, 1, 1, 0, 0, 1
), Points = c(51, 31, 49, 3, 13, 20, 13, 34, 13, 63), OppPoints = c(31, 
51, 3, 49, 20, 13, 34, 13, 63, 13), Total = c(82, 82, 52, 52, 
33, 33, 47, 47, 76, 76), MOV = c(-20, 20, -46, 46, 7, -7, 21, 
-21, 50, -50), Spread = c(-22, 22, -32, 32, 22.5, -22.5, 4, -4, 
40, -40)), row.names = c(1489L, 1490L, 297L, 298L, 1453L, 1454L, 
1915L, 1916L, 2121L, 2122L), class = "data.frame")

我需要创建一个新的数据框,其中包含唯一年份、日期和列名。我已经能够使用以下代码对 Points 和 OppPoints 列执行此操作,但是当我将其扩展到这两列之外时,我会遇到长度错误。所需的输出与下面完全相同,只是希望为 Total、MOV 和 Spread 添加更多行。

我尝试过的代码长度错误:

Data$Stat_Name <- colnames(Data[,6:10])

产生部分预期结果的代码:

Data$Stat_Name <- colnames(Data[,6:10])

部分期望结果:

structure(list(Year = c(2016, 2016, 2016, 2016, 2016, 2016, 2016, 
2016), Date = structure(c(17040, 17040, 17045, 17045, 17046, 
17046, 17047, 17047), class = "Date"), Stat_Name = c("Points", 
"OppPoints", "Points", "OppPoints", "Points", "OppPoints", "Points", 
"OppPoints")), row.names = c(1489L, 1490L, 297L, 298L, 355L, 
356L, 85L, 86L), class = "data.frame")

【问题讨论】:

  • 您可能需要paste(colnames(Data)[6:10], collapse = "_"),因为长度不等于行数
  • 是否有任何你想要实现的逻辑(虽然从帖子中不清楚)
  • 嘿@akrun,没有任何逻辑,除了我只需要一个包含年份、日期和列名列的所有唯一组合的数据框。我很快就能测试你上面的代码。您上面的代码是一个非常好的主意。然后我可以使用单独的行并获得所需的结果!谢谢!如果您将提交作为答案,我将接受!

标签: r dplyr


【解决方案1】:

这是dplyr 解决方案:

library(dplyr)
mutate(df, Stat_Name = paste0(names(df[c(6:7, 9:10)]), collapse = '_'))
   Team Code Opponent Code Year       Date HomeTeam Points OppPoints Total MOV Spread                   Stat_Name
1        107           277 2016 2016-08-27        0     51        31    82 -20  -22.0 Points_OppPoints_MOV_Spread
2        277           107 2016 2016-08-27        0     31        51    82  20   22.0 Points_OppPoints_MOV_Spread
3        129          1320 2016 2016-09-01        1     49         3    52 -46  -32.0 Points_OppPoints_MOV_Spread
4       1320           129 2016 2016-09-01        0      3        49    52  46   32.0 Points_OppPoints_MOV_Spread
5         27           694 2016 2016-09-01        0     13        20    33   7   22.5 Points_OppPoints_MOV_Spread
6        694            27 2016 2016-09-01        1     20        13    33  -7  -22.5 Points_OppPoints_MOV_Spread
7        231           306 2016 2016-09-01        1     13        34    47  21    4.0 Points_OppPoints_MOV_Spread
8        306           231 2016 2016-09-01        0     34        13    47 -21   -4.0 Points_OppPoints_MOV_Spread
9        314           465 2016 2016-09-01        0     13        63    76  50   40.0 Points_OppPoints_MOV_Spread
10       465           314 2016 2016-09-01        1     63        13    76 -50  -40.0 Points_OppPoints_MOV_Spread

【讨论】:

    【解决方案2】:

    我们可以在从 6 到 10 的列名称上创建一个带有 paste 的单列,因为行数将不等于作为向量传递的列名称(否则必须创建一个 list

    Data$Stat_Name <- paste(colnames(Data)[6:10], collapse = "_") 
    

    【讨论】:

      【解决方案3】:

      也许你需要它

      tibble(
        `Team Code` = c(107, 277, 129, 1320, 27, 694, 231, 306, 314, 465), 
        `Opponent Code` = c(277, 107, 1320, 129, 694, 27, 306, 231, 465, 314), 
        Year = c(2016, 2016, 2016, 2016,2016, 2016, 2016, 2016, 2016, 2016), 
        Date = structure(c(17040,17040, 17045, 17045, 17045, 17045, 17045, 17045, 17045, 17045),class = "Date"), 
        HomeTeam = c(0, 0, 1, 0, 0, 1, 1, 0, 0, 1), 
        Points = c(51, 31, 49, 3, 13, 20, 13, 34, 13, 63), 
        OppPoints = c(31,51, 3, 49, 20, 13, 34, 13, 63, 13), 
        Total = c(82, 82, 52, 52,33, 33, 47, 47, 76, 76), 
        MOV = c(-20, 20, -46, 46, 7, -7, 21, -21, 50, -50), 
        Spread = c(-22, 22, -32, 32, 22.5, -22.5, 4, -4,  40, -40)
      )  %>% 
        pivot_longer(cols=5:last_col(), names_to ="field", values_to ="val")
      

      【讨论】:

        猜你喜欢
        • 2016-12-08
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多