0

r-beginner here,

Is there a way to convert a table that I have:

> dput(head(ed, 5))
structure(list(series = c("Government expenditure on education, total (% of GDP)", 
"Government expenditure on education, total (% of GDP)", "Government expenditure on education, total (% of GDP)", 
"Government expenditure on education, total (% of GDP)", "Government expenditure on education, total (% of GDP)"
), country = c("Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", 
"Afghanistan"), year = c("2000", "2001", "2002", "2003", "2004"
), Value = c(NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_)), .Names = c("series", "country", "year", "Value"
), row.names = c(NA, 5L), class = "data.frame")

so that the parameters from df$series become column names, filled with values from df$Value, while Country and Year stay fixed to look something like this:

Country     Year  GovernmentExpenditure   Population  LiteracyRate [otherparams]
Afghanistan 2000        10%                21979923        60%
Australia   2010        15%                21979923        90%

etc.

I tried dcast() but it didn't work the way I wanted to.

Alternatively, is there a way to download it in the necessary format straight away from the world bank data?

Thank you!

jane
  • 33
  • 4
  • Please include a reproducible example. Use `dput(head(dat, 10))` or similar. We can't work with images... Also, include your desired output. Going back and forth 3 times to reach the desired goal is pretty inefficient. – lmo Jun 10 '18 at 21:55
  • Second, this is most likely a duplicate of either [this post](https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format) or [this post](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format) – lmo Jun 10 '18 at 21:59
  • yes, the second post was exactly the solution! thank you – jane Jun 10 '18 at 22:12
  • Great to hear. If you think this post may benefit future visitors, I can mark it a duplicate of that post. Otherwise, you could delete it. I'll leave it up to you. – lmo Jun 10 '18 at 22:16
  • Thanks for adding the reproducible example, by the by. This is quite helpful for those who would like to help you solve your problem. – lmo Jun 10 '18 at 22:18

0 Answers0