Suppose I have the following data set
d <- data.frame(1:31, 31:1)
names(d) <- c("cats", "dogs")
And I want to do a linear regression with columns as the dependent variables and the values as independent - if I had 2 columns, 1 columns named "Animals" containing 31 rows with the value "Cat" and 31 rows with the value "Dog" and 1 column named "values" with 62 rows containing the values 1:31-31:1 I think I could use
lm(Animals ~ values, data=df)
but is there a way to do this by just using column names as the first part of the expression?
Any help is much appreciated
d %>% pivot_longer(everything(), names_to = 'Animals', values_to = 'values') %>% {lm(values~ Animals, data = .)}
– akrun