0
votes

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
1
Please indent your code. That is, put four spaces at the beginning of each line of code.Frank
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
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
@JimmyT No let your answer there. He should have made that an answerHugo Dozois
I tried my best at cleaning it...Ricardo Saporta

1 Answers

1
votes

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)