Search code examples
rspread

Separate data from 1 column into 2 in r


I have a dataset of football teams and their Win/Loss results from 2009-2017. Currently the Wins and Losses are in the same column, one after the other, and I want to create a new column for the losses.

A sample of the data looks like:

Football <- data.frame (
    Season = rep ("2009", 10),
    Team = rep (c("ARI", "ARI", "ATL", "ATL", "BAL", "BAL", "BUF", "BUF", "CAR", "CAR")),
    Value = c(10, 6, 7, 9, 7, 9, 6, 10, 8, 8)
)

I would like the final output to show:

Season    Team    Wins    Losses
2009      ARI     10      6
2009      ATL     7       9
2009      BAL     7       9

and so on. There are also several other variables but the only one that changes for each Season/Team pair is the "Value".

I have tried several iterations of spread() and mutate() but they typically make many more columns (i.e. 2009.Wins, 2009.Losses, 2010.Wins, 2010.Losses) than I want.

Thanks for any help. I hope this post turns out alright, its my first time posting.

Cheers, Jeremy


Solution

  • We create a column of "Winloss" and then spread to 'wide' format

    library(tidyverse)
    Football %>%
      mutate(Winloss = rep(c("Win", "Loss"), n()/2)) %>%
      spread(Winloss, Value)
    #   Season Team Loss Win
    #1   2009  ARI    6  10
    #2   2009  ATL    9   7
    #3   2009  BAL    9   7
    #4   2009  BUF   10   6
    #5   2009  CAR    8   8
    

    data

    Football <- data.frame (
      Season = rep ("2009", 10),
      Team = rep (c("ARI", "ARI", "ATL", "ATL", "BAL", "BAL", "BUF", "BUF", "CAR", "CAR")),
      Value = c(10, 6, 7, 9, 7, 9, 6, 10, 8, 8)
    )