0

My current dataset :

order product
1  a
1  b
1  c
2  b
2  d
3  a
3  c
3  e

what I want

product order
a       1,3
b       1,2
c       1,3
d       2
e       3

I have tried cast, reshape, but they didn't work

HubertL
  • 19,246
  • 3
  • 32
  • 51
Xin Chang
  • 87
  • 1
  • 5

1 Answers1

0

I recently spent way too much time trying to do something similar. What you need here, I believe, is a list-column. The code below will do that, but it turns the order number into a character value.

library(tidyverse)
df <- tibble(order=c(1,1,1,2,2,3,3,3), product=c('a','b','c','b','d','a','c','e')) %>%
group_by(product) %>%
summarise(order=toString(.$order)) %>%
mutate(order=str_split(order, ', ')
Alex P
  • 1,574
  • 13
  • 28