2

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!

pogibas
  • 27,303
  • 19
  • 84
  • 117
Michael Matta
  • 394
  • 2
  • 16
  • Possible duplicate of [How can I spread repeated measures of multiple variables into wide format?](https://stackoverflow.com/questions/29775461/how-can-i-spread-repeated-measures-of-multiple-variables-into-wide-format) – duckmayr Aug 27 '18 at 21:14
  • My data set does not have a column with the name for each column to be spread. I would like to spread the data set in five column and rename them afterward. However, if this is not possible, I will delete my question. – Michael Matta Aug 27 '18 at 21:21

1 Answers1

2
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
AntoniosK
  • 15,991
  • 2
  • 19
  • 32