将新列添加到现有数据集

时间:2013-01-17 16:21:37

标签: r

  Date     City     Temp
  1/1/2012 Liverpool  10
  1/2/2012 Madrid     20
  1/3/2012 Milan      40
  1/4/2012 Istanbul   35
  1/5/2012 Munich      10

我需要在此数据集中添加具有县列名称的另一列。如果df $ City是马德里,那么Country必须是西班牙。我现在这是一个非常小的数据集,我需要能够以编程方式做这个瘦R?

我希望我的新数据框看起来像这样:

Date     City        Temp   Country
--------------------------------------
1/1/2012 Liverpool    10    England
1/2/2012 Madrid       20    Matrid
1/3/2012 Milan        40    Italy
1/4/2012 Istanbul     35    Turkey
1/5/2012 Munich       10    Germany

如何指示我如何在R中执行此操作?

2 个答案:

答案 0 :(得分:2)

提供确切数据的途中是:

 df <- read.table(text= " Date     City     Temp
                 1/1/2012 Liverpool  10
                 1/2/2012 Madrid     20
                 1/3/2012 Milan      40
                 1/4/2012 Istanbul   35
                  1/5/2012 Munich      10",header=TRUE)


df$Country <- ifelse(df$City == "Liverpool", "England",
                     ifelse(df$City == "Madrid", "Spain",
                         ifelse(df$City == "Milan", "Italy",
                           ifelse(df$City == "Istanbul", "Turkey", "Germany") )))

但是我假设你可能有更多的城市和国家,在这种情况下可能会有:

countrydf <- read.table(text= " City Country
                           Liverpool  England
                           Madrid     Spain
                           Milan      Italy
                           Istanbul   Turkey
                           Munich     Germany",header=TRUE,stringsAsFactors=FALSE)


merge(df,countrydf, by="City")

请注意:

查看了包maps,这可能对您有用

library(maps)
data(world.cities)
head(world.cities)

world.cities[world.cities$name == "Istanbul" ,]

答案 1 :(得分:0)

在不知道城市如何映射到您所处情况的国家/地区(即,它们是否映射到listvectordata.frame或其他完全相同的内容?),这很难猜猜你的答案是对的。这是一种方式,城市 - 国家/地区映射在列表中:

df <- read.table(text="Date     City     Temp
1/1/2012 Liverpool  10
1/2/2012 Madrid     20
1/3/2012 Milan      40
1/4/2012 Istanbul   35
1/5/2012 Munich      10", header=TRUE)

city.countries <- list(England=c('Liverpool', 'London'), 
                       Spain='Madrid', 
                       Italy='Milan', 
                       Turkey='Istanbul', 
                       Germany='Munich')

df <- transform(df, Country = with(stack(city.countries), ind[match(City, values)]))
#       Date      City Temp Country
# 1 1/1/2012 Liverpool   10 England
# 2 1/2/2012    Madrid   20   Spain
# 3 1/3/2012     Milan   40   Italy
# 4 1/4/2012  Istanbul   35  Turkey
# 5 1/5/2012    Munich   10 Germany