Reputation: 169
I have two columns of data referring to international abbreviations for countries (their ISO 2-Code and ISO 3-Code). I am going to use this as a "dictionary" to substitute the 2Codes for the 3Codes in another table using the gsubfn() function. I had to create the dictionary list by entering manually as follows:
d_list<-list('ABW'='AW','AFG'='AF','AGO'='AO','AIA'='AI','ALA'='AX','ALB'='AL','AND'='AD',
'ARE'='AE','ARG'='AR','ARM'='AM','ASM'='AS','ATG'='AG','AUS'='AU','AUT'='AT',
'AZE'='AZ','BAH'='AZ','BDI'='BI','BEL'='BE','BEN'='BJ','BFA'='BF','BGD'='BD',
'BGR'='BG','BHR'='BH','BIH'='BA','BLM'='BL','BLR'='BY','BLZ'='BZ','BMU'='BM',
'BOL'='BO','BRA'='BR','BRB'='BB','BRN'='BN')
d_list is a list of 212 elements ... one element per "=".
Is there a way to create d_list from a table or multiple tables that have the 2Code and 3Code in columns? ... would make this process so much faster and less error prone.
Upvotes: 1
Views: 55
Reputation: 93928
First, some test data:
df <- data.frame(code3 = c("ABW","AFG"), code2 = c("AW", "AF"))
If each of the 3-Code values have to be unique, then split
it:
split(df$code2, df$code3)
#$ABW
#[1] "AW"
#
#$AFG
#[1] "AF"
If not, make a list and set the names manually:
as.list(setNames(df$code2, df$code3))
#$ABW
#[1] "AW"
#
#$AFG
#[1] "AF"
Upvotes: 1