Pass normal arguments of global functions in Purrr map functions.

Example 1.
df_list <- map(filelist, read_excel) # WORKS!!!!
df_list <- map(filelist,read_excel(col_names = F)) #WHY NO WORK???

Example 2.
mtcars_sub <- mtcars[1:5,c("mpg", "hp", "disp")]
pmap(mtcars_sub, sum) #WORKS!!!!
pmap(mtcars_sub, sum(na.rm = F)) #WHY NO WORK???

Use ... to pass arguments to the mapped function.

library(purrr)
mtcars_sub <- mtcars[1:5,c("mpg", "hp", "disp")]
pmap(mtcars_sub, sum)
[[1]]
[1] 291

[[2]]
[1] 291

[[3]]
[1] 223.8

[[4]]
[1] 389.4

[[5]]
[1] 553.7

pmap(mtcars_sub, sum,na.rm = F) 
[[1]]
[1] 291

[[2]]
[1] 291

[[3]]
[1] 223.8

[[4]]
[1] 389.4

[[5]]
[1] 553.7

or use the ~ notation

df_list <- map(filelist, ~ read_excel(.x, col_names = F))

Thank you so much!
I didnt expect such a quick reply,
I hope I can help others too

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.