purrr将两个交叉点与公共列表相结合

purrr将两个交叉点与公共列表相结合,r,purrr,R,Purrr,我想使用purrr在表单后面创建一个路径 some_path/year/filename_with_year.xls 其中文件位于子目录中,该子目录与创建部分文件名所用的年份列表相同 到目前为止,我能够使用cross3和cross2创建路径的第一部分和文件名,但我无法以合理的方式将它们组合到完整路径中 library(tidyverse) year_list<-c(2008,2009,2010, 2011) country_list<-c("Andorra","Belarus")

我想使用
purrr
在表单后面创建一个路径

some_path/year/filename_with_year.xls
其中文件位于子目录中,该子目录与创建部分文件名所用的年份列表相同

到目前为止,我能够使用
cross3
cross2
创建路径的第一部分和文件名,但我无法以合理的方式将它们组合到完整路径中

library(tidyverse)

year_list<-c(2008,2009,2010, 2011)
country_list<-c("Andorra","Belarus")
remote_base_path<-"some_path"

filename<-cross3(country_list,year_list,".xls") %>%map(lift(paste0)) #create filename
filepath<-cross2(remote_base_path,year_list)%>%map(lift(file.path)) #create path


filename
filepath
库(tidyverse)

年份列表这可能不是最好的方法,但您可以尝试:

l <- cross(list(c(2008,2009,2010, 2011), 
                c("Andorra","Belarus"), 
                "some_path")) %>%
  map(set_names, c("year", "country","remote"))

map_chr(l, ~ glue("{file.path(.$remote, .$year)}/{.$country}_{.$year}.xls"))

[1] "some_path/2008/Andorra_2008.xls" "some_path/2009/Andorra_2009.xls"
[3] "some_path/2010/Andorra_2010.xls" "some_path/2011/Andorra_2011.xls"
[5] "some_path/2008/Belarus_2008.xls" "some_path/2009/Belarus_2009.xls"
[7] "some_path/2010/Belarus_2010.xls" "some_path/2011/Belarus_2011.xls"
l%
地图(设置名称,c(“年份”、“国家”、“远程”))
map_chr(l,~glue({file.path(.$remote,.$year)}/{.$country}{.$year}.xls”))
[1] “some_path/2008/andror_2008.xls”“some_path/2009/andror_2009.xls”
[3] “some_path/2010/andror_2010.xls”“some_path/2011/andror_2011.xls”
[5] “some_path/2008/白俄罗斯_2008.xls”“some_path/2009/白俄罗斯_2009.xls”
[7] “some_path/2010/白俄罗斯_2010.xls”“some_path/2011/白俄罗斯_2011.xls”

谢谢!将命名列表与map_chr和glue一起使用是一个很好的解决方案。完成任务,易于理解,并且易于扩展到更多贡献列表。