1

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]})
GPierre
  • 893
  • 9
  • 25
  • 3
    If you have `NA`s in the missing rows, just use `na.locf(df)` from the `zoo` package. – David Arenburg Feb 25 '15 at 16:27
  • Can you give a dput() of your initial data.frame? It's unclear to me how you could have something that looks like that. See [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Feb 25 '15 at 16:27
  • @DavidArenburg solution worked. I didn't know the function. tks. – GPierre Feb 25 '15 at 16:35

0 Answers0