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

2019-06-28 04:54发布

问题:

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.

回答1:

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  


标签: r lm magrittr