TeYaP
TeYaP

Reputation: 323

R loop with characters list

The problem here is similar to this previous one but here we do not need to do any computation but just to build lists

I have some list of world regions:

list.asia <- c("Central Asia", "Eastern Asia", "South-eastern Asia", "Southern Asia", "Western Asia")
list.africa <- c("Northern Africa", "Sub-Saharan Africa", "Eastern Africa", "Middle Africa", "Southern Africa", "Western Africa")

I use the R library("ISOcodes") to produce lists of countries with ISO Alpha 3 digits format as follow:

region <- subset(UN_M.49_Regions, Name %in% list.asia)
subset <- subset(UN_M.49_Countries, Code %in% unlist(strsplit(region$Children, ", ")))
subset$ISO_Alpha_3

This example, with the list.asiagives the expected result:

 [1] "AFG" "ARM" "AZE" "BHR" "BGD" "BTN" "BRN" "KHM" "CHN" "HKG" "MAC" "CYP" "PRK"
[14] "GEO" "IND" "IDN" "IRN" "IRQ" "ISR" "JPN" "JOR" "KAZ" "KWT" "KGZ" "LAO" "LBN"
[27] "MYS" "MDV" "MNG" "MMR" "NPL" "OMN" "PAK" "PHL" "QAT" "KOR" "SAU" "SGP" "LKA"
[40] "PSE" "SYR" "TJK" "THA" "TLS" "TUR" "TKM" "ARE" "UZB" "VNM" "YEM"

which can easily be saved as follow:

countries.list.asia <- subset$ISO_Alpha_3

The problem is that I have got a lot of regions and I would prefer to do a loop.

To keep it simple let's say that I only have 2 lists list.asia and list.africa. I regroup them in a new list.continent

list.continent <- c("list.asia","list.africa")

and then I "loop" the list production: (which does not work)

for(i in list.continent){
    list.loop <- sym(i)
    region <- subset(UN_M.49_Regions, Name %in% list.loop)
    subset <- subset(UN_M.49_Countries, Code %in% unlist(strsplit(region$Children, ", ")))
    paste("countries",list.loop, sep=".") <- subset$ISO_Alpha_3
    rm(region, subset, list.loop)
}

The expected results (in this case) are 2 new objects (class list) called countries.list.asia and countries.list.africa containing the ISO Alpha 3 digits codes of the countries present in these regions.

I tried to replace list.loop by !!list.loop or as.list(list.loop), but nothing works. Any Idea?

Upvotes: 1

Views: 218

Answers (1)

Parfait
Parfait

Reputation: 107642

Consider using an overall list and not attempt to save an object to global environment iteratively and use a function to return your needed output and avoid the need to remove helper objects. And in R, a list + function can be encapsulated with lapply (or its wrapper sapply used here for list names):

# NAMED LIST OF ACTUAL OBJECTS (NOT CHARACTER VECTOR)
list.continent <- list(list.asia = list.asia, list.africa = list.africa)

# BUILD NEW LIST OF SUBSETTED ITEMS
new_list.continent <- sapply(list.continent, function(item) {    
    region <- subset(UN_M.49_Regions, Name %in% item)
    sub <- subset(UN_M.49_Countries, Code %in% unlist(strsplit(region$Children, ", ")))

    return(sub$ISO_Alpha_3)
}, simplify = FALSE)

# SHOW OBJECT CONTENTS
new_list.continent$list.asia

new_list.continent$list.africa

Upvotes: 2

Related Questions