Split and Apply
Posted: Tue Oct 21, 2025 7:43 am
Hi,
I would like to split my mycars dataset into three subsets based on the cyl variable and apply linear regression on each of these datasets. Is there any way I can do this? Similar R code to achieve this is:
models1 = lapply(split(mtcars, mtcars$cyl), \(x)lm(mpg ~ disp, data = x))
tidy_list = lapply(models1, tidy)
tidy_list
I would like to split my mycars dataset into three subsets based on the cyl variable and apply linear regression on each of these datasets. Is there any way I can do this? Similar R code to achieve this is:
models1 = lapply(split(mtcars, mtcars$cyl), \(x)lm(mpg ~ disp, data = x))
tidy_list = lapply(models1, tidy)
tidy_list