user1471980
user1471980

Reputation: 10626

Add new column to the existing data set

  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

I need to add another column in this data set with County column name. If the df$City is Madrid, Country will need to be Spain. I now this is a very small data set, I need to be able to do this programatically thin R?

I would like my new data frame to look like this:

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

Any pointers how I would do this in R?

Upvotes: 0

Views: 4052

Answers (2)

Matthew Plourde
Matthew Plourde

Reputation: 44614

Without knowing how the cities are mapped to countries in your situation (i.e., are they mapped in a list, vector, data.frame, or something else altogether?), it's hard to guess what the right answer is for you. Here is one way, where the city-country mapping is in a list:

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

Upvotes: 0

user1317221_G
user1317221_G

Reputation: 15441

On way with your exact data provided is:

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

However I am assuming you may have more cities and countries, in which case something like:

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

note:

had a look in package maps, which could be useful to you

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

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

Upvotes: 2

Related Questions