简体   繁体   中英

How to recode and encode a country pair variable in R

I am trying to recode a variable for country pairs, eg an exporter EFG and an importeur ISR equals the country pair EFGISR. I need these pairs for a panel data analysis and therefore these country pairs have to be set to numeric variables. I am familiar to the as.numeric command, however recoding these variables back to the format seems to be a tough job. Do you guys know a better way to code it or a way to use the factor variable as a referene for a recode call ? I will have to use the plm package and the command make.pballanced().

Cheers and I would really appreciate your help!

edit:

idvar <- c(BRAWLD, BRAALB, BRADZA, BRAARG, BRAAUS, BRAAUT, BRABHR, BRAARM)

as.numeric(idvar)
[1] 108   2  30   5   7   8  12   6   9  15  11  17  23  19

as.factor(idvar)
[1] 108   2  30   5   7   8  12   6   9  15  11  17  23  19

This is the part where I would like to have again

idvar
BRAWLD, BRAALB, BRADZA, BRAARG, BRAAUS, BRAAUT, BRABHR, BRAARM

I am Heading my dataset here:

year    exp exp_iso                  imp imp_iso       nw       tv     nw_c nw_dc     tv_c tv_dc tv_total nw_total id_var
1996-BRAARE 1996 Brazil     BRA United Arab Emirates     ARE   563812  1245639   563812     0  1245639     0  1245639   563812 BRAARE
1996-BRAARG 1996 Brazil     BRA            Argentina     ARG 34006800 77508984 34006800     0 77508984     0 77508984 34006800 BRAARG
1996-BRAARM 1996 Brazil     BRA              Armenia     ARM    38398   70656    38398     0    70656     0    70656    38398 BRAARM
1996-BRAAUS 1996 Brazil     BRA            Australia     AUS  3213000  7864554  3213000     0  7864554     0  7864554  3213000 BRAAUS
1996-BRAAUT 1996 Brazil     BRA              Austria     AUT 11189578 25442560 11189578     0 25442560     0 25442560 11189578 BRAAUT
1996-BRABEL 1996 Brazil     BRA              Belgium     BEL 41944172 93179224 41944172     0 93179224     0 93179224 41944172 BRABEL

I found an appealing solution to the problem. Using the package countryodes provides a formula with which I could paste the charachter country codes as numeric codes using the countrycode = "iso3n".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM