【问题标题】:Consolidating Duplicate Rows By Creating New Columns [duplicate]通过创建新列合并重复行 [重复]
【发布时间】:2021-11-12 19:50:54
【问题描述】:

我有一个数据框,df。

Player Round SC
Dion Prestia Round 1 118
Dustin Martin Round 1 126
Trent Cotchin Round 1 94
Dion Prestia Round 2 91
Dustin Martin Round 2 86
Trent Cotchin Round 2 84
Dion Prestia Round 3 86
Dustin Martin Round 3 90
Trent Cotchin Round 3 90

我想将行合并为如下所示,但不知道如何实现。

Player Round 1 Round 2 Round 3
Dion Prestia 118 91 86
Dustin Martin 126 86 90
Trent Cotchin 94 84 90

我尝试使用以下方法没有成功:

reshape(df, idvar = "Player", timevar = "Round", direction = "wide")

因为它合并为 2 列,其中第 2 列被命名为 'SC.c("Round 1", "Round 2", "Round 3", "Round 4"' 并且所有行都显示 'NA' 值。

非常感谢您的帮助!

【问题讨论】:

  • tidyr::pivot_wider(df, names_from = Round, values_from = SC)

标签: r dataframe aggregate


【解决方案1】:
library(tidyr)

df %>% 
  pivot_wider(names_from = Round,values_from = SC)

# A tibble: 3 x 4
  Player        `Round 1` `Round 2` `Round 3`
  <chr>             <int>     <int>     <int>
1 Dion Prestia        118        91        86
2 Dustin Martin       126        86        90
3 Trent Cotchin        94        84        90

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 2018-09-17
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2016-12-15
    • 2014-12-09
    相关资源
    最近更新 更多