dplyr::do() requires named function?

后端 未结 2 561
梦毁少年i
梦毁少年i 2020-12-03 08:02

The following works fine:

library(dplyr) 
m <- function(df) {
  mod <- lm(Sepal.Length ~ Sepal.Width, data = df)
  pred <- predict(mod,newdata = df[         


        
相关标签:
2条回答
  • 2020-12-03 08:23

    You don't need an anonymous function:

    library(dplyr)
    iris %>%
      group_by(Species) %>%
      do({
        mod <- lm(Sepal.Length ~ Sepal.Width, data = .)
        pred <- predict(mod, newdata = .["Sepal.Width"])
        data.frame(., pred)
      })
    
    0 讨论(0)
  • 2020-12-03 08:27

    You can't get rid of the ..

    iris %>%
      group_by(Species) %>%
      do((function(df) {
        mod <- lm(Sepal.Length ~ Sepal.Width, data = df)
        pred <- predict(mod,newdata = df["Sepal.Width"])
        data.frame(df,pred)
      })(.))
    

    That will work. The . is necessary. The . is love. Keep the ..

    0 讨论(0)
提交回复
热议问题