0
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'purrr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'stringr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
mtcars = as_tibble(mtcars)
cars_nested = mtcars %>% group_by(cyl) %>% nest()

cars_fitted = cars_nested %>% 
  mutate(fit = map(data, function(x) lm(mpg ~ disp, data = x)))

reprex 包(v0.3.0)于 2020 年 7 月 29 日创建

当我在 Rstudio 中运行此代码时,我收到以下错误。有什么帮助吗?

> mtcars = as_tibble(mtcars)
> cars_nested = mtcars %>% group_by(cyl) %>% nest()
> cars_fitted = cars_nested %>% 
+   mutate(fit = map(data, function(x) lm(mpg ~ disp, data = x)))
Error: Problem with `mutate()` input `fit`.
x object 'disp' not found
i Input `fit` is `map(data, function(x) lm(mpg ~ disp, data = x))`.
i The error occured in group 1: cyl = 4.
Run `rlang::last_error()` to see where the error occurred. ```
4

1 回答 1

0

感谢您的建议。我找到了为什么会收到这些错误。在前面的代码中,我在不知不觉中将 mtcars 嵌套到了同一个 mtcars 数据集。所以只有 cyl 和 data 列。这就是我得到错误的原因。

于 2020-07-29T14:43:40.307 回答