Formula with variables whose name match a certain criteria

Hi everyone,

I have created dummies for each of the 70 countries in my dataset, and would like to run a regression on some "normal" variables, and also the 70 dummies I have created.

How could I create a function where instead of writing all the variable names one by one (as I did below), I could summon some variables individually (e.g value1 in the example below), and then all the variables that match a certain criteria (e.g starts with "dummy") ?

library(lmtest)

df <- data.frame( id = 1:4, year = 1993:1996, dummy1 = c(1,0,0,0), 
dummy2 = c(0,1,0,0), dummy3 = c(0,0,1,0), 
dummy4 = c(0,0,0,1), value1 = c(1,2,3,4), value2 = c(2,8,12,5))

df

model <- lm(value2 ~ value1 + dummy1 + dummy2 + dummy3 + dummy4, data = df)
summary(model)

That would look something like this:

model1 <- lm(value2 ~ value1 + starts_with("dummy"))

but I get the following error

"Error: No tidyselect variables were registered"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.