0

Hi i have panel data and would like to reshape or cast my Indicator name column from long to wide format. currently all the columns are in long format, Year(1960-2011), Country Name (all the countries in the world), Indicator name (varying by different indicators) and Value(individual values corresponding to year, indicator name and country name). How can i do this can someone help please. I would like the various indicators to be in the wide format with the corresponding value below it and on the other columns year and country name. Please help

Indicator.Name  Year    Country
GDP             1960    USA
GDP             1960    UK




Country Name    Year    GDP    PPP    HHH
USA             1960    7       9      10
Uk              1960    9       10     NA
World           1960    7       5      3
Africa          1960    3       7      NA
ManafQ
  • 21
  • 3
  • 4
    Please indent your code. That is, put four spaces at the beginning of each line of code. – Frank May 14 '13 at 18:34
  • 1
    I'll second what @Frank said... your code is all but unreadable. However, look at the `reshape2` package. Specifically, `dcast`. You'll want something like `dcast(yourdata, Country + Year ~ Indicator.Name, value.var='Value')`. – Justin May 14 '13 at 18:43
  • I dont know how to do that frank, sorry – ManafQ May 14 '13 at 18:48
  • well its been processing your code Justin for the last two minutes, so smth must be goin on – ManafQ May 14 '13 at 18:52
  • 1
    Its my first time on this website, thanks for the friendly attitude hugo, not everyone is at your level of competence. Anyways, Justin i cant thank you enough, it worked like a charm. Frank thank you for your patience. – ManafQ May 14 '13 at 19:00
  • This is actually my first time answering a question that isn't my own. Given that @Justin solution came in a minute before mine, should I delete my answer? – JimmyT May 14 '13 at 19:05
  • 2
    @JimmyT No let your answer there. He should have made that an answer – Hugo Dozois May 14 '13 at 19:05
  • Jimmy T, thank you, that is what another user suggested as well, it worked great. – ManafQ May 14 '13 at 19:09
  • @user2380366 thanks...if you can check the answer to mark this as solved would be great. It takes a bit of time to get used to the way folks communicate on stackoverflow. As a relative newbie, best advice I can give is to not be argumentative...at least until you become an expert :) – JimmyT May 14 '13 at 19:11
  • Keep your answer Jimmy T. Thank you – ManafQ May 14 '13 at 19:17
  • @user2380366 I'm glad you found a solution. I'm also relatively new here. I think the hope with a Q&A site like this is that your question will also be useful to others in the future. Both askers and answerers contribute to the site in the process. That's why clear formatting is nice. Some kind soul may come by to fix it, if you don't. – Frank May 14 '13 at 20:43
  • 2
    I tried my best at cleaning it... – Ricardo Saporta May 14 '13 at 22:57
  • @Frank, ok i guess it was just a simple indent like you said, i fixed it and posted another question on ggplot panel data (elsewhere). I'm just getting the hang of it like you. thanks for your help – ManafQ May 15 '13 at 03:23

1 Answers1

1

try using dcast from reshape2 like below:

library(reshape2)
indicator <- c('PPP','PPP','GDP','GDP')
country.name <- c('USA','UK','USA','UK')
year <- c(1960,1961,1960,1961)
value <- c(5,7,8,9)
d <- data.frame(indicator, country.name, year, value)
d1 <- dcast(d, country.name + year ~ indicator)
JimmyT
  • 1,099
  • 4
  • 10
  • 15