1

This is an example of long form data that I have:

ID   value1     value2
1   4.333333   3.833333
1   4.333333   3.333333
2   4.583333   5.500000
2   3.916667   3.750000
3   4.500000   4.666667
3   4.333333   4.500000

I need to convert it to wide form like so:

ID   value1A    value1B   value2A    value2B
1   4.333333   3.833333  4.333333   3.833333 
2   4.333333   3.333333  4.333333   3.833333
3   4.583333   5.500000  4.333333   3.833333

To accomplish this using the tidy verse, I did the following:

ds_spread <- gather(ds,condition, value, contains("value")) %>% 
  separate(condition, into = c("t1", "t2")) %>% 
  arrange(ID) %>% 
  group_by(ID) %>% 
  mutate(rownum = row_number()) %>% 
  select(-t1,-t2) %>% 
  spread(rownum, value)

I ended up getting the outcome I was looking for. However, was this by happenstance? Is the gather %>% separate %>% arrange %>% mutate %>% select necessary? Is there a more elegant solution?

Michael
  • 41,989
  • 11
  • 82
  • 128
pablom
  • 105
  • 2
  • 5

1 Answers1

2

Here is one option.

library(dplyr)
library(tidyr)

dat2 <- dat %>%
  group_by(ID) %>%
  mutate(Letter = LETTERS[row_number()]) %>%
  ungroup() %>%
  gather(Value, Number, starts_with("value")) %>%
  unite(Col, Value, Letter, sep = "") %>%
  spread(Col, Number) 
dat2
# # A tibble: 3 x 5
#      ID value1A value1B value2A value2B
#   <int>   <dbl>   <dbl>   <dbl>   <dbl>
# 1     1    4.33    4.33    3.83    3.33
# 2     2    4.58    3.92    5.50    3.75
# 3     3    4.50    4.33    4.67    4.50

DATA

dat <- read.table(text = "ID   value1     value2
1   4.333333   3.833333
1   4.333333   3.333333
2   4.583333   5.500000
2   3.916667   3.750000
3   4.500000   4.666667
3   4.333333   4.500000",
                  header = TRUE, stringsAsFactors = FALSE)
www
  • 38,575
  • 12
  • 48
  • 84
  • This worked too! I'll definitely use this as an example. I'm trying to wrap my head as to why the example provided here did not work: https://stackoverflow.com/a/43966036/3358272 – pablom Apr 25 '18 at 03:21