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?