0

I have a very long script of files that are read into R, and manipulated in many ways. I'm not too familiar with the language, but feel this can all be simplified.

For each file (or later, object it turns into) I have 6 versions, and the names always only differ in terms of these numbers (x1,x2,...,x6, or p1_yes,p2_yes,...,p6_yes)

I'm not sure how to apply loops over the various pieces of code to simplify it. Merging all those objects together 1-6 would not work due to some data manipulations I have to make that differ between them.

Just 3 examples:

agri1 <-  raster("p1_agri.tif")
agri2 <-  raster("p2_agri.tif")
agri3 <-  raster("p3_agri.tif")
agri4 <-  raster("p4_agri.tif")
agri5 <-  raster("p5_agri.tif")
agri6 <-  raster("p6_agri.tif")
pas1 <- as.data.frame(pas1)
pas2 <- as.data.frame(pas2)
pas3 <- as.data.frame(pas3)
pas4 <- as.data.frame(pas4)
pas5 <- as.data.frame(pas5)
pas6 <- as.data.frame(pas6)
apglm1 <- glm(test~agri+post,family="binomial",data=alldata1)
apglm2 <- glm(test~agri+post,family="binomial",data=alldata2)
apglm3 <- glm(test~agri+post,family="binomial",data=alldata3)
apglm4 <- glm(test~agri+post,family="binomial",data=alldata4)
apglm5 <- glm(test~agri+post,family="binomial",data=alldata5)
apglm6 <- glm(test~agri+post,family="binomial",data=alldata6)
Beardedant
  • 134
  • 8
  • 3
    I'd strongly suggest using a `list`. See my answer at [How to make a list of data frames?](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for some code to get you started and plenty of justification. – Gregor Thomas May 30 '23 at 20:25
  • 1
    There’s virtually never a reason to have numbered objects instead of lists or vectors. – Konrad Rudolph May 30 '23 at 20:32

1 Answers1

1
agri1 <-  raster("p1_agri.tif")
agri2 <-  raster("p2_agri.tif")
agri3 <-  raster("p3_agri.tif")
agri4 <-  raster("p4_agri.tif")
agri5 <-  raster("p5_agri.tif")
agri6 <-  raster("p6_agri.tif")

You can achieve a similar result for an arbitrary number of files in the following fashion:

fns <- sprintf('p%s_agri.tif', 1:6) # specify number of files here
results_list <- lapply(fns, raster) 
results_list <- setNames(results_list, fns)
pas1 <- as.data.frame(pas1)
pas2 <- as.data.frame(pas2)
pas3 <- as.data.frame(pas3)
pas4 <- as.data.frame(pas4)
pas5 <- as.data.frame(pas5)
pas6 <- as.data.frame(pas6)

The same effect can be achieved with an arbitrary number of variables in the following fashion (with the caveat that using get() is considered a dodgy R programming practice, you've been warned)

vns <- sprintf('pas%s', 1:6)
results_list <- lapply(vns, function(x) as.data.frame(get(x)))
results_list results_list <- setNames(results_list, vns)
apglm1 <- glm(test~agri+post,family="binomial",data=alldata1)
apglm2 <- glm(test~agri+post,family="binomial",data=alldata2)
apglm3 <- glm(test~agri+post,family="binomial",data=alldata3)
apglm4 <- glm(test~agri+post,family="binomial",data=alldata4)
apglm5 <- glm(test~agri+post,family="binomial",data=alldata5)
apglm6 <- glm(test~agri+post,family="binomial",data=alldata6)

We can use the same approach as above (same caveat about using get() applies)...

vns <- sprintf('alldata%s', 1:6)
results_list <- lapply(vns, function(x) glm(test ~ agri + post, family = "binomial", data = get(x])))
results_list <- setNames(results_list, vns)

All of these methods will yield a list of results (results_list) with elements named according to the convention specified in your original snippets.

You should probably also read: How do I make a list of data frames? where a number of approaches and best practices are outlined in detail.

br00t
  • 1,440
  • 8
  • 10