I have a large dataset that I need to convert to a long format from a wide format. This should be fairly simple, and there are many examples of how to do this in this forum. However, in this case, I also need to split the column headings used in a wide format and create a column for each of them in a long format.
Data set example
data <- data.frame("East2010"=1:3, "West2010"=4:6, "East2011"=7:9, "West2011"=5:7)
data
East.2010 West.2010 East.2011 West.2011
1 1 4 7 5
2 2 5 8 6
3 3 6 9 7
I want something like this
Site Year Response
East 2010 1
East 2010 2
East 2010 3
West 2010 4
West 2010 5
West 2010 6
East 2011 7
East 2011 8
East 2011 9
West 2011 5
West 2011 6
West 2011 7
I looked at a lot of examples on this forum that will melt data for conversion to long format and others that separate columns on a separator, but I could not get these two projects to work together.
source
share