Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `%>%` with `lm` and `rbind`

Tags:

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.

like image 903
Pascal Avatar asked Mar 21 '18 09:03

Pascal


1 Answers

We need to extract the data and . represents the data object

Z %>% 
  filter(d == 1) %>% 
  lm(y ~ t, data = .)
#Call:
#lm(formula = y ~ t, data = .)

#Coefficients:
#(Intercept)            t  
#          2            1  

Within the summarise/mutate/group_by and other tidyverse functions, we can simply pass the column name. Here, either we need to get the columns from within the environment of data or create a list output in summarise

library(magrittr)    
Z %>%
  filter(d ==1 ) %>%
  summarise(lmout = list(lm(y ~ t))) %>%
  pull(lmout) %>%
  extract2(1)
#Call:
#lm(formula = y ~ t)

#Coefficients:
#(Intercept)            t  
#          2            1  
like image 112
akrun Avatar answered Sep 22 '22 13:09

akrun