0

I have a dataframe, 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

I want to consolidate the rows to look like shown below, but am not sure how to achieve it.

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

I have tried using the following with no success:

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

as it combines into 2 columns, where column 2 is named 'SC.c("Round 1", "Round 2", "Round 3", "Round 4"' and all rows show a 'NA' value.

Thank you so much for your help!

Fonti
  • 133
  • 1
  • 9

1 Answers1

0
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
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32