我有两个表:gdp
和country_details
,如下所示:
gdp
Countryname 2013 2014 2015
America $2015 NA $502
Uganda NA $123 $234
Nigeria $546 NA NA
列2013
,2014
和2015
提供了各个国家/地区的gdp
值。
country_details
Storename Year
America 2015
Nigeria 2014
Uganda 2013
Nigeria 2013
America 2014
Uganda 2015
我想查看gdp
中Storename
中提到的国家/地区的gdp
值Year
country_details
country_details
中所提及的Storename=Countryname
他们到表Year
。
我知道我们可以加入2013
这两个表,但我不知道列2014
,2015
和{{1}的{{1}}列的地图值如何}}
答案 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"]
}
}