r - dplyr::do() requires named function? -
the next works fine:
library(dplyr) m <- function(df) { mod <- lm(sepal.length ~ sepal.width, info = df) pred <- predict(mod,newdata = df["sepal.width"]) data.frame(df,pred) } iris %>% group_by(species) %>% do(m(.))
i thought work if used anonymous function, not:
iris %>% group_by(species) %>% do(function(df) { mod <- lm(sepal.length ~ sepal.width, info = df) pred <- predict(mod,newdata = df["sepal.width"]) data.frame(df,pred) }) error: results not info frames @ positions: 1, 2, 3
you don't need anonymous function:
library(dplyr) iris %>% group_by(species) %>% do({ mod <- lm(sepal.length ~ sepal.width, info = .) pred <- predict(mod, newdata = .["sepal.width"]) data.frame(., pred) })
r dplyr
No comments:
Post a Comment