2

I have four list of plots (each list has 8 plots), where these lists have been compiled as a list. So, basically; list of 4 lists, where each sub-list has 8 plots.

Now, I need 8 lists as outcome; wherein first element of each of 4 lists generated as a separate list.

Can anyone help over here that how can I use either "lapply" or "for loop" to get required outcome?

I tried following block, but there was no output [ABC -> "NULL"].

PlotsA <- list(Plot1.png, Plot2.png,..., Plot8.png)
PlotsB <- list(Plot9.png, Plot10.png,..., Plot16.png) 
PlotsC <- list(Plot17.png, Plot18.png,..., Plot24.png)
PlotsD <- list(Plot25.png, Plot26.png,..., Plot32.png)

All_Plots <- list(PlotsA, PlotsB, PlotsC, PlotsD)
 
ABC <- for (i in seq_along(All_Plots)){
  for (j in length(All_Plots[[i]])){
    x = list(All_Plots[[i]][j])
    x 
  }
}
ABC

The outcome I am looking for is...

Outcome1
[1]
Plot1.png, Plot9.png, Plot17.png, Plot25.png
Outcome2
[2]
Plot2.png, Plot10.png, Plot18.png, Plot26.png
...

and so on.

Further processing include grid of 4 plots (for eg., Plot1.png, Plot9.png, Plot17.png, and Plot25.png). I can do this once I get the aforementioned outcomes.

M--
  • 25,431
  • 8
  • 61
  • 93
Samarth
  • 25
  • 4
  • It's not clear to me if (e.g.) `Plot1.png` is a PNG file or a recorded plot in R. I think you're hoping to get `Outcome#` plots that are 4-wide combinations of your `Plot#` plots, is that right? If so, look at the `patchwork` package, it might be as easy as `library(patchwork); Outcome1 <- Plot1.png + Plot9.png + Plot17.png + Plot25.png` – r2evans Apr 08 '23 at 18:01
  • `lapply(1: length(plots), function(i){ ggsave(plot = plots[[i]], filename = paste(names(plots)[i], ".pdf"), width = 15) })` try this where plots is the list of plot you have created i have done for pdf replace with .png – PesKchan Apr 08 '23 at 18:02
  • Plot1.png and so on are recorded plots in R generated by looping over 8 excel files. – Samarth Apr 08 '23 at 18:03
  • @Samarth any dummy data you can provide that would be helpful ..you can `dput(df)` – PesKchan Apr 08 '23 at 18:04

1 Answers1

1

purrr::transpose() does exactly what you want — takes a list of n elements, each with length m, and returns a list of m elements each with length n.

I used strings here as stand-ins for demonstration purposes, but it’ll work just as well with actual plots.

library(purrr)

transpose(All_Plots)

Result:


[[1]]
[[1]][[1]]
[1] "Plot1.png"

[[1]][[2]]
[1] "Plot9.png"

[[1]][[3]]
[1] "Plot17.png"

[[1]][[4]]
[1] "Plot25.png"


[[2]]
[[2]][[1]]
[1] "Plot2.png"

[[2]][[2]]
[1] "Plot10.png"

[[2]][[3]]
[1] "Plot18.png"

[[2]][[4]]
[1] "Plot26.png"


# ... 

[[8]]
[[8]][[1]]
[1] "Plot8.png"

[[8]][[2]]
[1] "Plot16.png"

[[8]][[3]]
[1] "Plot24.png"

[[8]][[4]]
[1] "Plot32.png"

Example data:

PlotsA <- as.list(paste0("Plot", 1:8, ".png"))
PlotsB <- as.list(paste0("Plot", 9:16, ".png"))
PlotsC <- as.list(paste0("Plot", 17:24, ".png")) 
PlotsD <- as.list(paste0("Plot", 25:32, ".png"))

All_Plots <- list(PlotsA, PlotsB, PlotsC, PlotsD)
zephryl
  • 14,633
  • 3
  • 11
  • 30