I want to re-shape this data frame using pivot_longer -
# A tibble: 5 x 7
PizzaNumber Topping_1 Category_1 Topping_2 Category_2 Topping_3 Category_3
<int> <fct> <fct> <fct> <fct> <fct> <fct>
1 1 cheese vegetarian ham carnivorous tomato vegetarian
2 2 spinach vegetarian tomato vegetarian NA NA
3 3 pineapple vegetarian cheese vegetarian ham carnivorous
4 4 cheese vegetarian tomato vegetarian NA NA
5 5 beef carnivorous NA NA NA NA
Into the following long format -
# A tibble: 11 x 3
PizzaNumber Topping Category
<int> <fct> <fct>
1 1 cheese vegetarian
2 1 ham carnivorous
3 1 tomato vegetarian
4 2 spinach vegetarian
5 2 tomato vegetarian
6 3 pineapple vegetarian
7 3 cheese vegetarian
8 3 ham carnivorous
9 4 cheese vegetarian
10 4 tomato vegetarian
11 5 beef carnivorous
Can somebody help me with the code to achieve this? So far my attempts have resulted in a jumbled mess.
Code for wide data frame is as follows -
> dput(widedata)
structure(list(PizzaNumber = c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 5L), Topping = structure(c(2L, 3L, 6L, 5L, 6L, 4L, 2L,
3L, 2L, 6L, 1L), .Label = c("beef", "cheese", "ham", "pineapple",
"spinach", "tomato"), class = "factor"), Category = structure(c(2L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L), .Label = c("carnivorous",
"vegetarian"), class = "factor")), class = "data.frame", row.names = c(NA,
-11L))