-1

I have multiple data tables that im trying to run the same command for, and sicne i dont want to type each one for every command, I'm trying to loop it. They have the same variables.

say they are named data_2018q1 data_2018q2 data_2019q1 data_2019q2 i want to run

lm(VD4020~V2010+V2007, data = )for all of them, without having to run multiple lm()but no idea how to. for reproducible example, use cars: cars1 = cars cars2 = cars*2 cars3 = cars*3 lm(speed~dist, data= )

for the three of them. Thanks ahead!

Lelleo
  • 59
  • 6
  • 3
    First step: Put your datasets in a list. Second step: `lapply(yourlist, function(DF) lm(y ~ x, data = DF))`. – Roland Oct 12 '21 at 06:26

1 Answers1

1

You could put them into a list and use lapply.

cars1 <- cars
cars2 <- cars*2
cars3 <- cars*3
cars_list <- list(cars1, cars2, cars3)
lapply(cars_list, function(x) lm(speed~dist, data = x))
Tjn25
  • 685
  • 5
  • 18