I have a dataframe Z
looking like
t x y d
0 1 2 1
1 2 3 1
2 3 4 1
0 1 2 2
1 2 3 2
2 3 4 2
with d
being a factor column. I know want to fit a linear model with lm
to y
over t
for both factors in d
and add it as a new column to the dataframe.
I tried
Z %>%
filter(d == 1) %>%
lm(y ~ t)
but this gives me an error saying "Error in as.data.frame.default(data) :
cannot coerce class ""formula"" to a data.frame"
. But
lm(y ~ t, data = Z)
works fine. Any help would be appreciated.