0

I have a dataset like that:

RULE | GENERATION
A | 1
B | 1
C | 1
D | 2

I would like this output:

1 | 2
A | D
B |
C |

At this time i tried spread, aggregate and also a lot of functions, but still no have the desire result. I want to group by "GENERATION" and make its categories the column name of the new dataset where each column have the values with same order of the first dataset.

Thanks.

1 Answers1

1

Something like this?

library(tidyverse)
 df<-data.frame(x=c(letters[1:4]),y=c(1,1,1,2))

 df%>%
   group_by(y)%>%
   mutate(num=row_number())%>%
   spread(y,x)%>%
   select(-num)
# A tibble: 3 x 2
  `1`   `2`  
  <fct> <fct>
1 a     d    
2 b     NA   
3 c     NA   
jyjek
  • 2,627
  • 11
  • 23