I am looking for a solution to write my code (below) in a shorter way (It works like this).
I have created a function called calculs_geo () and I am using it with different arguments.
Maybe with an apply() or purrr::map() ?
Many thanks in advance !
calculs_geo(maille = "France",filiere = "ensemble", champ = "ensemble")
calculs_geo(maille = "FrancedeProvince",filiere = "ensemble", champ = "ensemble")
calculs_geo(maille = "REG",filiere = "ensemble", champ = "ensemble")
calculs_geo(maille = "DEP",filiere = "ensemble", champ = "ensemble")
calculs_geo(maille = "ZE2020",filiere = "ensemble", champ = "ensemble")
calculs_geo(maille = "France",filiere="logistique", champ = "ensemble")
calculs_geo(maille = "FrancedeProvince",filiere="logistique", champ = "ensemble")
calculs_geo(maille = "REG",filiere="logistique", champ = "ensemble")
calculs_geo(maille = "DEP",filiere="logistique", champ = "ensemble")
calculs_geo(maille = "ZE2020",filiere="logistique", champ = "ensemble")
calculs_geo(maille = "France",filiere="logistique",champ="6_domaines")
calculs_geo(maille = "FrancedeProvince",filiere="logistique",champ="6_domaines")
calculs_geo(maille = "REG",filiere="logistique",champ="6_domaines")
calculs_geo(maille = "DEP",filiere="logistique",champ="6_domaines")
calculs_geo(maille = "ZE2020",filiere="logistique",champ="6_domaines")
CodePudding user response:
We can create 3 vectors of variable names and use purrr::pmap
mailles<-rep(c("France", "FrancedeProvince", "REG", "DEP", "ZE2020"), times = 3)
filieres<-rep(c("ensemble", "logistique", 'logistique'), each = 5)
champs<-rep(c('ensemble', 'ensemble', "6_domaines"), each = 5)
purrr::pmap(list(mailles, filieres, champs), function(x,y,z) caluculs_geo(x, y, z))
This may be simplified into purrr::pmap(list(mailles, filieres, champs), caluculus_geo)
, hard to tell without seeing the definition of calculus_geo
Illustrative example:
calculs_geo<-function(x, y, z) paste(x, y, z, sep = ' ')
purrr::pmap(list(mailles, filieres, champs), calculs_geo)
[[1]]
[1] "France ensemble ensemble"
[[2]]
[1] "FrancedeProvince ensemble ensemble"
[[3]]
[1] "REG ensemble ensemble"
[[4]]
[1] "DEP ensemble ensemble"
[[5]]
[1] "ZE2020 ensemble ensemble"
[[6]]
[1] "France logistique ensemble"
[[7]]
[1] "FrancedeProvince logistique ensemble"
[[8]]
[1] "REG logistique ensemble"
[[9]]
[1] "DEP logistique ensemble"
[[10]]
[1] "ZE2020 logistique ensemble"
[[11]]
[1] "France logistique 6_domaines"
[[12]]
[1] "FrancedeProvince logistique 6_domaines"
[[13]]
[1] "REG logistique 6_domaines"
[[14]]
[1] "DEP logistique 6_domaines"
[[15]]
[1] "ZE2020 logistique 6_domaines"