Hello everyone, I hope you're doing well. I need help transforming a specific dataframe into a long format. The data pertains to a consumer survey for two or more products. The collection is done via an online form, and I import the generated Excel file into R as a dataframe at the end of the survey.
The structure of the dataframe is as follows, with the variables in sequence:
- id (chr): Consumer identification
- sample (chr): Identifier of the sample evaluated in the first position
- question_1 (num): First question about the sample evaluated in the first position
- question_2 (num): Second question about the sample evaluated in the first position
- question_3 (chr): Third question about the sample evaluated in the first position
- sample: (chr) Identifier of the sample evaluated in the second position
- question_1 (num): First question about the sample evaluated in the second position
- question_2 (num): Second question about the sample evaluated in the second position
- question_3 (chr): Third question about the sample evaluated in the second position
The above example dataframe has 9 columns, with some of them having the same column names but different observations because they are different samples. R automatically renames repeated column names (example: sample, sample.1), despite the question name being the same.
The final dataframe that I need in long format would have the following variables in sequence:
id, sample, question_1, question_2, question_3
I tried using tidyr to code with pivot_longer (based on the example from R4DS bellow), but I couldn't solve the issue. Can anyone help me?
billboard |>
pivot_longer(
cols = starts_with("wk"),
names_to = "week",
values_to = "rank"
)