如何更改data.frame中列的内容

如何更改data.frame中列的内容

问题描述:

我正在使用来自世界发展指标(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

这将适用于角色或因素。

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) } 

使用子集:

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

最简单的,特别是如果你有很多的名字改变,可能是把你的对应关系表中一个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代码,这是更标准,比国名的两个数据集。