0

My question might be weird. I am still a beginner in R Programming...looking for efficient ways of coding. I am about to learn how to use the apply-functions in R... So... if I have a list with lists of data frames and want to apply linear models to each of this data frames in the list with less lines as possible... which apply function would this be? Or is it not recommanded to work with lists of lists?

Imagine this example:

iris_split1 <- split(iris[,1:4]*0.5, iris$Species)
iris_split2 <- split(iris[,1:4]*0.85, iris$Species)
iris_split3 <- split(iris, iris$Species)
list_iris <- list(year2017=iris_split1,
                  year2018 =iris_split2,
                  year2020 = iris_split3)

# I could do this for only a list of data frames
lm1 <- lapply(iris_split1, function(x) lm(x[,1] ~ x[,2], data = x))

# But I could not found a solution to run it for lists of lists with data frames? 

Thanks, Nadine

Nadiine El Nino
  • 339
  • 1
  • 6
  • Use two calls to `lapply`. Like `lapply(list_iris, function(x) lapply(x, function(y) lm(y[,1] ~ y[,2], data = y)))`. – Ian Campbell Jun 10 '21 at 15:49
  • Yes, I just found another solution myself! lapply(unlist(list_iris,recursive = F), function(x) lm(x[,1] ~ x[,2], data = x)) How can I tell which is more efficient? – Nadiine El Nino Jun 10 '21 at 15:51
  • The lapply/lapply version makes a list of list of results. In contrast, yours is unlisted, losing the structure of the results. – Ian Campbell Jun 10 '21 at 15:52
  • You could check out the corresponding tidyverse functions for iteration https://r4ds.had.co.nz/iteration.html. With nested tibbles, i.e. data frames but somewhat cooler, running multiple linear regressions becomes quite easy. In fact, this was part of a class I taught this week (maybe [these lecture notes](https://yards.albert-rapp.de/lm-with-lists.html) may help you. – AlbertRapp Jun 10 '21 at 16:13
  • Thanks, I will check it out - looks promising! Haven't worked with tidyverse so much yet! – Nadiine El Nino Jun 23 '21 at 08:55

0 Answers0