如何在R中连接两个表,其中一个表中的值在另一个表中是可变的?

时间:2017-03-13 05:38:51

标签: r join

我有两个表:gdpcountry_details,如下所示:

gdp

Countryname    2013       2014      2015
America       $2015         NA      $502
Uganda           NA       $123      $234
Nigeria        $546         NA        NA

201320142015提供了各个国家/地区的gdp值。

country_details

 Storename                Year
   America                2015
   Nigeria                2014
   Uganda                 2013
   Nigeria                2013
   America                2014
   Uganda                 2015

我想查看gdpStorename中提到的国家/地区的gdpYear country_details country_details中所提及的Storename=Countryname他们到表Year。 我知道我们可以加入2013这两个表,但我不知道列20142015和{{1}的{{1}}列的地图值如何}}

2 个答案:

答案 0 :(得分:1)

正如@thelatemail所说,将gdp数据重塑为更有利的格式将使你从长远来看变得轻松无忧。您需要做的就是

require(reshape2)
gdp.l <- melt(gdp,id.vars = 'Countryname', variable.name = 'Year', value.name = 'GDP')

答案 1 :(得分:0)

## Input data

gdp<-data.frame(Countryname=c("America","Uganda", "Nigeria"), 
            x2013=c(12,13,14), x2014=c(14,35,20), x2015=c(22,44,55))

country_details<-data.frame(Storename=c("America", "Nigeria", "Uganda", "Nigeria", "America", "Uganda"),
                        Year=c(2015,2014,2013,2013,2014,2015))

# Use the match() function to get all the rows in 
# the same order as the countries in your country_details data frame
# match gives the row number of gdp that matches up with each country
# name in the country_details data frame

# Then we extract those rows using [] and store it to "matches"

matches<-gdp[match(country_details$Storename, gdp$Countryname),]

# We can use cbind to lock these two together

output<-cbind(country_details, matches)

# If you really want just one column for your country and year combination
# you can use this

for (i in 1:nrow(output)){

if (output$Year[i]=="2013"){
  output$YearGDP[i]<-output[i,"x2013"]
}

if (output$Year[i]=="2014"){
  output$YearGDP[i]<-output[i,"x2014"]
}

if (output$Year[i]=="2015"){
    output$YearGDP[i]<-output[i,"x2015"]
}
}