I have a dataset in the wide format which looks like this:
header 1 x x x y y z z
header 2 1 2 3 1 2 1 2
2015 3.0 4.0 2.0 3.1 2.1 2.3 2.1
2016 2.0 4.4 1.0 2.1 3.1 5.3 0.1
...
I want to change it to A long format which looks like this:
header 1 variable 1 2 3
2015 x 3.0 4.0 2.0
2015 y 3.1 2.1 N/A
2015 Z 2.3 2.1 N/A
2016 ...
This was my approach:
- concatenate header 1 and header two with a separator
- use
melt()
function, and - split the two
But, this is not working as the melt
function changes my characters to factor and then I can't split them. Can anyone help or suggest a better approach?