I am trying to convert my data from wide to long using tidyverse's gather
command (I would prefer to avoid melting/casting.) However, my data has two matching rows that I want to use in the conversion.
`2018 Data` Codes `Code1` `Code2` `Code3`
1 ID Names Code1Name Code2Name Code3Name
2 1110 Crop… 0 0 0
3 1120 Anim… 0 0 0
4 1131 Timb… 0 0 0
You'll notice that the data is strange in that there are two rows with variable names, but the second row is being treated as the first row of observations. The codes and codenames match, but I haven't found a way to do a proper gather command that matches them.
Currently, I have something like df_tall<-gather(df_wide, key="Codes", value="CodeValue")
However, this does not match the names properly.
The ideal data I want to get is below:
ID Names Codes CodeValue
1 1110 Crop… Code1Name Code1 0
2 1110 Crop… Code2Name Code2 0
3 1110 Crop… Code3Name Code3 0
Any ideas of how I can achieve this?
There is a dearth of questions on StackOverflow for converting from wide to long, but I didn't have luck finding any with a similar question (please correct me if I've overlooked something).