1

I have a dataset that looks something like this:

Name Result
X1   Red
X2   Blue
X2   Green
X3   Red
X2   Yellow
X3   Blue

and I would like the values in the "Name" column to turn into their own columns with their corresponding "Result" value

X1  X2     X3
Red NA     NA
NA  Blue   NA
NA  Green  NA 
NA  NA     Red
NA  Yellow NA
NA  NA     Blue

I've looked into some options in tidyr (such as separate), but I haven't had any luck.

any help is appreciated. thank you :)

markus
  • 25,843
  • 5
  • 39
  • 58
Sarah
  • 411
  • 4
  • 14

1 Answers1

2

We create a row number and then spread

library(tidyverse)
df1 %>% 
   mutate(rn = row_number()) %>%
   spread(Name, Result) %>%
   select(-rn)
akrun
  • 874,273
  • 37
  • 540
  • 662