I have a data frame with three columns:
responseid = c(rep("R_cJ6dbDcou", 6), rep("R_a3LWPfGC", 6), rep("R_e3b9tIJo", 6))
year = c(rep(2022, 6), rep(2022, 6), rep(2022, 6))
code = c(000, 0500, 0033, 0036, 0102, 0486, 000, 0500, 0032,
0039, 0101, 0466, 000, 0500, 0012, 0049, 0111, 0446)
Data = data.frame(responseid , year , code)
I would like it to appear with each row as a unique responseid, and each code related to that responseid as a column:
| responseid | code1 | code2 | code3 | code4 | code5 | code6 |
|---|---|---|---|---|---|---|
| R_cJ6dbDcou | 000 | 0500 | 0033 | 0036 | 0102 | 0486 |
| R_a3LWPfGC | 000 | 0500 | 0032 | 0039 | 0101 | 0466 |
I've tried grouping by responseid then pivot_wider, but this doesn't seem to work. I end up with each responseid as a column and a list in each cell with the codes for that responseid.
I didn't get from your question whether you expect each column to contain the same code or not.
If yes, the following should work:
Else, this should work: