如何重塑WDI数据

时间:2013-10-06 09:30:18

标签: r reshape

我需要重塑从worldbank数据库下载的一些数据。但是我遇到了一些困难。

目标是看起来像这样:

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

3 个答案:

答案 0 :(得分:3)

使用reshape2很容易实现。

require(reshape2)
dcast(wdi[,-2], year ~ iso2c, value.var = 'NY.GDP.PCAP.CD')

EDIT。哎呀,我没有看到Ananda Mahto发布的评论同样的解决方案。 Anand,如果你发表你的评论作为答案,我会删除我的。

答案 1 :(得分:3)

再次在电脑前...所以这是一个更新。

正如我的评论中所提到的,来自“reshape2”的dcast非常方便。如果您正在进行重塑步骤,则可以从基础R中的xtabs获得类似的功能。

x <- xtabs(NY.GDP.PCAP.CD ~ year + iso2c, wdi)
head(x)
#       iso2c
# year         CH        DE       US
#   1980 17807.34 11746.404 12179.56
#   1981 16219.91  9879.457 13526.19
#   1982 16527.46  9593.657 13932.68
#   1983 16398.24  9545.859 15000.09
#   1984 15601.26  9012.479 16539.38
#   1985 15748.95  9125.121 17588.81

xtabs创建matrix class“xtabs”,因此要获得data.frame,请将输出包装在as.data.frame.matrix中。

head(as.data.frame.matrix(x))
#            CH        DE       US
# 1980 17807.34 11746.404 12179.56
# 1981 16219.91  9879.457 13526.19
# 1982 16527.46  9593.657 13932.68
# 1983 16398.24  9545.859 15000.09
# 1984 15601.26  9012.479 16539.38
# 1985 15748.95  9125.121 17588.81

要回答您在评论中提出的其他问题:但是,通过仅使用重塑功能,这不是一种更聪明的方式将数据直接放到正确的格式中。答案是“是的。只需在原来的reshape尝试中交换您用于”idvar“和”timevar“的内容。”

y <- reshape(wdi[-2], direction = "wide", idvar="year", timevar="iso2c")
## Optional step to clean up the resulting names
names(y) <- gsub("NY.GDP.PCAP.CD.", "", names(y))
head(y)
#   year       CH       DE       US
# 1 2010 70572.66 40163.82 46615.51
# 2 2009 65790.07 40275.25 45305.05
# 3 2008 68555.37 44132.04 46759.56
# 4 2007 59663.77 40402.99 46349.12
# 5 2006 54140.50 35237.60 44622.64
# 6 2005 51734.30 33542.78 42516.39

使用reshape函数时,有时忽略参数名称的“id”和“time”部分会有所帮助,而是考虑它们的去向。 ID变量通常构成一列,时间变量通常分散,每次一列。因此,即使我们可能将“country”视为实际的ID变量,但对于您想要的数据格式,它更像是一个时间变量。

希望这有帮助,即使你已经接受了答案:)

答案 2 :(得分:0)

这是基础R解决方案。

# renames the NY.GDP column and drops all but two columns
trans_one <- function(dat) {
    newcol <- dat[1, "iso2c"]
    idx <- which(colnames(dat)=="NY.GDP.PCAP.CD")
    colnames(dat)[[idx]] <- newcol
    dat <- dat[,c(newcol, "year")]
    dat
}

# split by country
sp <- split(wdi, wdi$iso2c)

# merge 
fun <- function(x,y) {
    merge(x, trans_one(y), by="year", all=TRUE)
}   
Reduce(fun, x=tail(sp, -1), init=trans_one(sp[[1]]))

然而,reshape2现在看起来更直接。