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

蹲街弑〆低调 提交于 2019-12-10 17:26:26

问题


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  


来源:https://stackoverflow.com/questions/49402641/using-with-lm-and-rbind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!