I've got data set showing each persons' candy preferences. Each person has multiple rows to display all of the candy that they like, like shown below.
Name ID Candy
Alex 101 Kit Kat
Alex 101 Twix
Sam 102 Reeses
Charlie 103 Skittles
Charlie 103 Twix
Charlie 103 Kit Kat
Charlie 103 Hershey
Nia 104 M&M
Nia 104 Skittles
Nia 104 Reeses
...
I want to convert this to a person-level data set where the Candy columns are limited to the first 3 listed.
Name ID Candy 1 Candy 2 Candy 3
Alex 101 Kit Kat Twix NA
Sam 102 Reeses NA NA
Charlie 103 Skittles Twix Kit Kat
Nia 104 M&M Skittles Reeses
I originally tried to use the Spread function, but I believe it doesn't work because the number of rows per person is variable.
Is there a way to achieve this in R? I'm not quite sure how to approach this problem.