0
  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 是马德里,则国家/地区必须是西班牙。我现在这是一个非常小的数据集,我需要能够以编程方式做这个瘦 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 中做到这一点?

4

2 回答 2

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" ,]
于 2013-01-17T16:33:24.833 回答
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
于 2013-01-17T17:42:58.577 回答