R code making 1 column into multiple columns with their unique ID
Currently stuck on a data wrangling question in R. So far I've tried variations of this code using tidyverse package, columns 5 and 6 here were the rating and the user:
df[,5:6] %%
pivot_wider(names_from = question,
values_from = rating,
names_sep = .) %%
unnest(cols = everything())- df_reformat
Each column will be the question ID and the rows are the scores for each user, ideally clustered by group.
Data structure needed:
repID | user | Customer question 1 | Customer question 2 | .... | Customer question N |
---|---|---|---|---|---|
de812 | 717201 | 5 | 4 | ------------------- | 5 |
de812 | 82200 | 5 | 4 | ------------------- | 3 |
Current data structure:
zone | repID | GroupID | Customer question | Rating | user |
---|---|---|---|---|---|
A001 | de812 | levelw001 | CSX001 | 4 | 717201 |
A001 | de812 | levelw001 | X400m1 | 4 | 82200 |
A001 | de812 | leveln0075 | bf001s | 5 | 201135 |
A001 | de812 | levelw001 | X400m1 | 5 | 717201 |
A001 | de812 | leveln0075 | bf001s | 5 | 82200 |
With the rows of each question column contain the rating scores 1-7.
Many of the dimensions are different so it's gets complicated. For example, sales rep 1 has 2 groups (with varying number of users between groups1 and 2) and questions asked (for sales rep 1, group 1, there were 5 unique question IDs but varying number of users). I dropped two questions so the dimensions fit but ideally I'd like to run my CFA with them included.
Thank you anyone that has the willingness to attempt to help!
Topic dplyr data-wrangling data-formats data-cleaning r
Category Data Science