Consider this simple tibble
data <- tibble(y = c(1,2,3,4,5),
var1 = c(20,19,20,30,10),
var2 = c(21,13,21,31,10),
boo1 = c(40,40,40,40,2),
boo2 = c(1,2,34,40,2))
# A tibble: 5 x 5
y var1 var2 boo1 boo2
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 20 21 40 1
2 2 19 13 40 2
3 3 20 21 40 34
4 4 30 31 40 40
5 5 10 10 2 2
I would like to be able to define a lm formula based on some regex condition on my tibble colum names.
Something like
data %>%
names() %>%
str_subset(.,regex('var')) %>%
paste('y ~', ., collapse = '+') %>%
as.formula() %>%
lm(., data = data)
which fails at several steps. For instance, the paste step gives "y ~ var1+y ~ var2"and the lm step simply does not work.
What should I try instead?
Thanks!