【发布时间】: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)