2012-01-11 60 views
0

我正在使用來自世界發展指標(WDI)的數據並希望將此數據與其他一些數據合併。我的問題是兩個數據集中國家名稱的拼寫有所不同。如何更改國家/地區變量?如何更改data.frame中列的內容

library('WDI') 
df <- WDI(country="all", indicator= c("NY.GDP.MKTP.CD", "EN.ATM.CO2E.KD.GD", 'SE.TER.ENRR'), start=1998, end=2011, extra=FALSE) 

head(df) 
     country iso2c year NY.GDP.MKTP.CD EN.ATM.CO2E.KD.GD SE.TER.ENRR 
99 ArabWorld 1A 1998 575369488074   1.365953   NA 
100 ArabWorld 1A 1999 627550544566   1.355583 19.54259 
101 ArabWorld 1A 2000 723111925659   1.476619   NA 
102 ArabWorld 1A 2001 703688747656   1.412750   NA 
103 ArabWorld 1A 2002 713021728054   1.413733   NA 
104 ArabWorld 1A 2003 803017236111   1.469197   NA 

如何將ArabWorld更改爲阿拉伯世界?

有很多名字我需要改變,所以使用row.numbers這樣做不會給我足夠的靈活性。我想要的東西與Stata中的replace函數類似。

+2

哪個國家是阿拉伯世界?你可能會發現'car'包中的'recode'函數很有用,或者把它改成'factor'而不是一個字符矢量,然後修改'levels'。除此之外,還要查看用於替換字符向量的'?sub'。 – James 2012-01-11 13:52:54

+0

看來問題是關於更改專欄的問題,所以我希望你不要在編輯中冒犯。 – 2012-01-11 17:00:16

回答

4

這將適用於角色或因素。

df$country <- sub("ArabWorld", "Arab World", df$country) 

這相當於:

> df[,1] <- sub("ArabWorld", "Arab World", df[,1]) 
> head(df) 
     country iso2c year NY.GDP.MKTP.CD EN.ATM.CO2E.KD.GD 
99 Arab World 1A 1998 575369488074   1.365953 
100 Arab World 1A 1999 627550544566   1.355583 
101 Arab World 1A 2000 723111925659   1.476619 
102 Arab World 1A 2001 703688747656   1.412750 

如果您創建具有所需的更改,您可以通過循環來改變他們一個數據幀。請注意,我已經更新了這一點,以便它顯示了,這樣他們就可以正確地傳遞給sub如何在該列中輸入括號:

name.cng <- data.frame(orig = c("AntiguaandBarbuda", "AmericanSamoa", 
            "EastAsia&Pacific\\(developingonly\\)", 
            "Europe&CentralAsia\\(developingonly\\)", 
            "UnitedArabEmirates"), 
          spaced=c("Antigua and Barbuda", "American Samoa", 
            "East Asia & Pacific (developing only)", 
            "Europe&CentralAsia (developing only)", 
             "United Arab Emirates")) 
for (i in 1:NROW(name.cng)){ 
     df$country <- sub(name.cng[i,1], name.cng[i,2], df$country) } 
0

使用子集:

df[df[, "country"] == "ArabWorld", "country"] <- "Arab World" 

head(df) 
    country iso2c year NY.GDP.MKTP.CD EN.ATM.CO2E.KD.GD SE.TER.ENRR 
99 Arab World 1A 1998 575369488074   1.365953   NA 
100 Arab World 1A 1999 627550544566   1.355583 19.54259 
101 Arab World 1A 2000 723111925659   1.476619   NA 
102 Arab World 1A 2001 703688747656   1.412750   NA 
103 Arab World 1A 2002 713021728054   1.413733   NA 
104 Arab World 1A 2003 803017236111   1.469197   NA 
+1

如果數據包含缺失值(這裏不是這種情況,但經常發生),「df [which(df [,」country「] ==」ArabWorld「),」country「]'更安全。 – 2012-01-11 14:15:48

+0

(+1)好點。 – mbask 2012-01-11 17:58:04

1

最簡單的,特別是如果你有很多的名字改變,可能是把你的對應關係表中一個data.frame,並與數據加入,與merge命令。 舉例來說,如果你想改變朝鮮的名稱:

# Correspondance table 
countries <- data.frame(
    iso2c = c("KR", "KP"), 
    country = c("South Korea", "North Korea") 
) 

# Join the data.frames 
d <- merge(df, countries, by="iso2c", all.x=TRUE) 
# Compute the new country name 
d$country <- ifelse(is.na(d$country.y), as.character(d$country.x), as.character(d$country.y)) 
# Remove the columns we no longer need 
d <- d[, setdiff(names(d), c("country.x", "country.y"))] 

# Check that the result looks correct 
head(d) 
head(d[ d$iso2c %in% c("KR", "KP"), ]) 

但是,它可能會更安全的加入對國家ISO代碼,這是更標準,比國名的兩個數據集。