How can I flatten data that is already in a data frame in long format? (I am not sure I have correctly defined my problem but the example below should be clear).
Initial data frame:
Region District Data
1 Arusha Arusha (Rural) Area ('000'ha)
2 Production ('000'tons)
3 Yield (tons/ha)
4 Arusha (Urban) Area ('000'ha)
5 Production ('000'tons)
6 Yield (tons/ha)
7 Karatu Area ('000'ha)
8 Production ('000'tons)
9 Yield (tons/ha)
Desired data frame
Region District Data
1 Arusha Arusha (Rural) Area ('000'ha)
2 Arusha Arusha (Rural) Production ('000'tons)
3 Arusha Arusha (Rural) Yield (tons/ha)
4 Arusha Arusha (Urban) Area ('000'ha)
5 Arusha Arusha (Urban) Production ('000'tons)
6 Arusha Arusha (Urban) Yield (tons/ha)
7 Arusha Karatu Area ('000'ha)
8 Arusha Karatu Production ('000'tons)
9 Arusha Karatu Yield (tons/ha)
Tentative
production<-structure(list(Region = c("Arusha", "", "", "", "", "", "", "",
""), District = structure(c("Arusha (Rural)", "", "", "Arusha (Urban)",
"", "", "Karatu", "", ""), .Dim = c(9L, 1L), .Dimnames = list(
NULL, "District")), Data = c(" Area ('000'ha)", " Production ('000'tons)",
" Yield (tons/ha)", " Area ('000'ha)", " Production ('000'tons)",
" Yield (tons/ha)", " Area ('000'ha)", " Production ('000'tons)",
" Yield (tons/ha)")), .Names = c("Region", "District", "Data"
), row.names = c(NA, 9L), class = "data.frame")
apply(production[2], 1,function(x){if(x=="") x=x[-1]})