我需要重塑从worldbank数据库下载的一些数据。但是我有一些困难。如何重塑WDI数据
的目标是,它看起来像这样:
year CH DE US
1980 17383.38 11746.40 12179.56
1981 15833.74 9879.46 13526.19
1982 16133.97 9593.66 13932.68
1983 16007.82 9545.86 15000.09
1984 15229.82 9012.48 16539.38
我用下面的代码来下载数据。 WDI和RJSONO包是必需的。
wdi <- WDI(country = c("CH","DE","US"), indicator = "NY.GDP.PCAP.CD" ,start = 1980, end = 2010, extra = F)
然后我重塑了以下方法:
wdi2 <- reshape(wdi, direction = "wide", timevar="year", v.names="NY.GDP.PCAP.CD", idvar="country", drop="iso2c")
输出不匹配我的应该怎么看的期望:
> wdi2
country NY.GDP.PCAP.CD.2010 NY.GDP.PCAP.CD.2009 NY.GDP.PCAP.CD.2008
1 Switzerland 70572.66 65790.07 68555.37
32 Germany 40163.82 40275.25 44132.04
63 United States 46615.51 45305.05 46759.56 ...
这一个是好一点,但还是没有我想要的:
> t(wdi2)
1 32 63
country "Switzerland" "Germany" "United States"
NY.GDP.PCAP.CD.2010 "70572.66" "40163.82" "46615.51"
NY.GDP.PCAP.CD.2009 "65790.07" "40275.25" "45305.05"
NY.GDP.PCAP.CD.2008 "68555.37" "44132.04" "46759.56"
NY.GDP.PCAP.CD.2007 "59663.77" "40402.99" "46349.12"
的WDI对象看起来是这样的:
> wdi
iso2c country NY.GDP.PCAP.CD year
1 CH Switzerland 70572.657 2010
2 CH Switzerland 65790.067 2009
3 CH Switzerland 68555.372 2008
4 CH Switzerland 59663.770 2007
...
30 CH Switzerland 16219.906 1981
31 CH Switzerland 17807.340 1980
32 DE Germany 40163.817 2010
33 DE Germany 40275.251 2009
34 DE Germany 44132.042 2008
...
62 DE Germany 11746.404 1980
63 US United States 46615.511 2010
64 US United States 45305.052 2009
在我的手机,但它好像你可能只是在寻找'吨(WDI2 [-1])'之后就可以重新添加列名称并清理行名称。 – A5C1D2H2I1M1N2O1R2T1
@Ananda Mahto Thx。但是,通过仅使用重塑函数,它们不是一种更智能的方法,可以将数据直接转换为正确的格式吗? – SWR
正如我所说,在我的手机:)请张贴您的wdi物体的几行,我会看到想到什么。 – A5C1D2H2I1M1N2O1R2T1