Simple use of Purr path

R learner but used to different languages

How would I convert the following loop to a map loop

Regions <- c("Cheshire","Derbyshire")

for (x in Regions){
ProcessList(x,"ABC")
}

Thanks

Simplest would be:

library(purrr)
regions <- c('Cheshire', 'Derbyshire')
map(regions, ProcessList, 'ABC')

Thanks but how is x passed to the ProcessList

ie in original the definition of ProcessList would be
ProcessList <- function(region,sVal)
So with the map function how is region being passed ?
thanks

implicit:

map(regions, ProcessList, 'ABC')

explicit:

map(regions,
    ~ProcessList(.x,'ABC'))

Thanks and sorry for being so dumb but how do I get .x

If I try the following :-

ProcessList<- function(region,sVal){
path <- paste0(region,sVal)
}

ProcessAllLists <- function(AllRegions){
map(AllRegions,ProcessList(.x,"df1"))
}

ProcessAllLists()

I get an error

Error in paste0(region, sVal) : object '.x' not found
Called from: paste0(region, sVal)

the .x syntax is enabled by the tilde symbol ~

Missed that sorry

Works great thanks everyone

All works fine unless I use a single element in the list then I get

Error in -map(AllRegions, ~ProcessList(.x, "df1")) :
invalid argument to unary operator

For additional support please provide a reprex of your issue

Sorted it thanks for all your help

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.