2013-10-06 103 views
2

我需要重塑從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 
+1

在我的手機,但它好像你可能只是在尋找'噸(WDI2 [-1])'之後就可以重新添加列名稱並清理行名稱。 – A5C1D2H2I1M1N2O1R2T1

+0

@Ananda Mahto Thx。但是,通過僅使用重塑函數,它們不是一種更智能的方法,可以將數據直接轉換爲正確的格式嗎? – SWR

+0

正如我所說,在我的手機:)請張貼您的wdi物體的幾行,我會看到想到什麼。 – A5C1D2H2I1M1N2O1R2T1

回答

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創建的class 「XTABS」 一matrix,因此要獲得一個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」和參數名的「時間」的部分,而是想想他們去哪裏。 ID變量通常組成一列,時間變量通常分佈很廣,每次一列。因此,儘管我們可能會將「country」看作實際的ID變量,但對於您想要的數據格式而言,它更像是一個時間變量。

希望這有助於,即使你已經接受了答案:)

3

這是很容易使用reshape2來實現。

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

編輯。糟糕,我沒有看到Ananda Mahto發佈的評論採用相同的解決方案。阿南德,如果你發表你的評論作爲答案,我會刪除我的。

+0

沒問題。無法驗證,所以我不想發佈任何內容。投擲前是否需要放置不需要的列? – A5C1D2H2I1M1N2O1R2T1

+0

其實沒有。但是,我想明確表示您將有效地放下一列。 – Ramnath

+0

用兩個基本的R解決方案添加了一個答案來補充這個問題。 :) – A5C1D2H2I1M1N2O1R2T1

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現在看起來更直截了當。

+0

這似乎是一個非常痛苦的基地R方法。請參閱[我的答案](http://stackoverflow.com/a/19211791/1270695),以獲得'xtabs'和'reshape'解決方案,它們都是base R的一部分。 – A5C1D2H2I1M1N2O1R2T1