如何在R中使用countrycode包的codelist_面板

如何在R中使用countrycode包的codelist_面板,r,panel-data,country-codes,R,Panel Data,Country Codes,我试图使用R中countrycode包中的面板代码列表,但不知道如何包含年份 我有一个带有国家名称和年份的数据集,我想添加相应的cow代码作为新变量或替换国家名称。我只是不知道怎么做 我的数据看起来有点像这样: country <- c("Australia", "Australia", "Canada", "Belgium") year <- c(1995, 2000, 1880, 1885) a <- c( 5.55, 4.5, 6.75, 8.3) data<-

我试图使用R中countrycode包中的面板代码列表,但不知道如何包含年份

我有一个带有国家名称和年份的数据集,我想添加相应的cow代码作为新变量或替换国家名称。我只是不知道怎么做

我的数据看起来有点像这样:

country <- c("Australia", "Australia", "Canada", "Belgium")
year <- c(1995, 2000, 1880, 1885)
a <- c( 5.55, 4.5, 6.75, 8.3)

data<- data.frame(country, year, a)

country您需要合并数据和
codelist\u面板

输入数据-我更改了国家/地区列的列名,因此我们可以在下面的
参数中通过
将其与年份列一起指定

dat <- data.frame(country.name.en = country, year, a)

我编辑了你的问题,因为不需要
cbind
。您应该避免这种情况,因为
cbind
强制列
year
a
为字符,因为矩阵只能包含一种类型。
dat <- data.frame(country.name.en = country, year, a)
library(countrycode)
merge(dat,
      codelist_panel[, c("country.name.en", "year", "cown")],
      by = c('year', 'country.name.en'))
#  year country.name.en    a cown
#1 1880          Canada 6.75   NA
#2 1885         Belgium 8.30  211
#3 1995       Australia 5.55  900
#4 2000       Australia 4.50  900