Search code examples
rdplyrspread

Trying to make data wide 4 columns at once


I am trying to make my data wider than it already is. I tried using spread but I would like to spread 4 variables at one time. A sample dataset is:

  df <- data.frame(Year <- c("2017","2018"),
                           ID <- c(1,1),
                           Score <- c("21","32"),
                           Score2 <- c("24","20"),
                           Score3 <- c("33", "26"),
                           Score4 <- c("25","32"))

     Year ID Score Score2 Score3 Score4
   1 2017  1    21     24     33     25
   2 2018  1    32     20     26     32

I would like to make it wide so that all scores from both years are on 1 row like this:

  Year Score Score2 Score3 Score4 Year2 Score18 Score218 Score318 Score418
1 2017    21     24     33     25  2018      32       20       26       32

The "Year2" column is not totally necessary but I would like some way to decipher between 2017 and 2018.

Any help or guidance would be appreciated! Thanks!


Solution

  • Another approach could be

    library(tidyverse)
    library(splitstackshape)
    
    df %>%
      group_by(ID) %>%
      summarise_all(funs(toString)) %>%
      cSplit(names(.)[-1], ",")
    

    Output is:

       ID Year_1 Year_2 Score_1 Score_2 Score2_1 Score2_2 Score3_1 Score3_2 Score4_1 Score4_2
    1:  1   2017   2018      21      32       24       20       33       26       25       32
    

    Sample data:

    df <- data.frame(Year = c("2017","2018"),
                     ID = c(1,1),
                     Score = c("21","32"),
                     Score2 = c("24","20"),
                     Score3 = c("33", "26"),
                     Score4 = c("25","32"))