0

I have read several topics in which users would advise against the use of the get() and assign() functions as it alters readibility of the code, which I cannot deny (for instance here). But then I am a bit lost as to be good practice.

I have an example currently : I have a pdf with tables about several countries, which I import as a dataframe CF_Brut_pdftools. My list of countries is ListePays=c("US","JP","DE","FR","UK","IT","CA","ZE","NL","NO","ES","SE","CH")

I have already a list of pages to use for each country. So I do

for (Pays in ListePays){
  assign(paste("CF",Pays,sep="_"),CF_Brut_pdftools[get(paste("Page",Pays,sep="_"))])
}

And the I have some data manipulation, for isntance :

for (Pays in ListePays2){
  assign(paste("CF",CF,"2",sep="_"),as.list(strsplit(get(paste("CF",Pays,sep="_")), "\n") [[1]]))
}

That will continue for several steps

What would be better options than this way ?

It is not straightforward to have a MWE in this case, but I can work on one if it helps.

Anthony Martin
  • 767
  • 1
  • 9
  • 28
  • Can't you skip out the first step and use `lapply` to run the function on each element of your list instead of a loop? Like `lapply(ListePays, function(x) as.list(strsplit(x, "\n") [[1]])` ? – Allan Cameron Sep 15 '20 at 10:22
  • Yes, a reproducible example would be helpful. – Ronak Shah Sep 16 '20 at 03:35

0 Answers0