Search code examples
rfunctiondplyrspread

Spread one column in multiple columns


I have one column "m" that contains multiple values associated with one subject (ID). I need to spread the values in this column in 5 different columns to obtain the second table that I provided below. I also need to associate names to those columns.

f <- read.table(header = TRUE, text = "
    Scale ID            m
1       1  1    0.4089795
2       1  1  0.001041055
3       1  1    0.1843616
4       1  1   0.03398921
5       1  1        FALSE
6       3  1    0.1179424
7       3  1    0.3569155
8       3  1    0.2006204
9       3  1   0.04024855
10      3  1        FALSE
")  

Here's what the output should look like

  ID Scale         x           y         z          a     b
1  1     1 0.4089795 0.001041055 0.1843616 0.03398921 FALSE
2  1     3 0.1179424 0.356915500 0.2006204 0.04024855 FALSE

Thanks for any help!


Solution

  • df <- read.table(header = TRUE, text = "
    Scale ID            m
    1       1  1    0.4089795
    2       1  1  0.001041055
    3       1  1    0.1843616
    4       1  1   0.03398921
    5       1  1        FALSE
    6       3  1    0.1179424
    7       3  1    0.3569155
    8       3  1    0.2006204
    9       3  1   0.04024855
    10      3  1        FALSE
    ") 
    
    library(tidyverse)
    
    df %>%
      group_by(Scale, ID) %>%                     # for each combination of Scale and ID
      mutate(names = c("x","y","z","a","b")) %>%  # add column names
      ungroup() %>%                               # forget the grouping
      spread(-Scale, -ID) %>%                     # reshape data
      select(Scale, ID, x, y, z, a, b)            # order columns
    
    # # A tibble: 2 x 7
    #   Scale    ID x         y           z         a          b    
    #   <int> <int> <fct>     <fct>       <fct>     <fct>      <fct>
    # 1     1     1 0.4089795 0.001041055 0.1843616 0.03398921 FALSE
    # 2     3     1 0.1179424 0.3569155   0.2006204 0.04024855 FALSE