Add Column of Predicted Values to Data Frame with dplyr

后端 未结 4 973
太阳男子
太阳男子 2020-12-14 23:47

I have a data frame with a column of models and I am trying to add a column of predicted values to it. A minimal example is :

exampleTable <- data.frame(x          


        
4条回答
  •  庸人自扰
    2020-12-15 00:29

    Eh, this is only slightly better:

    answer = 
      exampleTable %>%
      group_by(groups) %>%
      do(lm( y ~ x , data = .) %>% 
           predict %>% 
           data_frame(prediction = .)) %>%
      bind_cols(exampleTable)
    

    I was hoping this would work but it didn't.

    answer = 
      exampleTable %>%
      group_by(groups) %>%
      mutate(prediction = 
               lm( y ~ x , data = .) %>% 
               predict)
    

提交回复
热议问题