I have the following dataset
order_id <- c(2,2,2,3,3,3)
product_name <- c("banana","Garlic Powder","carrots","milk","banana","Coconut Butter")
(df <- data.frame(order_id,product_name))
order_id product_name
1 2 banana
2 2 Garlic Powder
3 2 carrots
4 3 milk
5 3 banana
6 3 Coconut Butter
And I want to make this wider where each of the products related to that order_id is a row.
I used the following function
df%>%
pivot_wider(names_from =product_name,values_from = product_name)
which gives me this
order_id banana `Garlic Powder` carrots milk `Coconut Butter`
<dbl> <chr> <chr> <chr> <chr> <chr>
2 banana Garlic Powder carrots NA NA
3 banana NA NA milk Coconut Butter
But I want it to be in the order that the products are in before widening the dataset, and I don't need to have column names associated with those values. I need something like this
order_id item1 item2 item3
2 banana Garlic Powder carrots
3 milk banana Coconut Butter
How do I go about doing this?