简体   繁体   中英

Error in eval(predvars, data, env) : object not found

I have a problem with a nested data frame on R. I used gapminder for an exercise, I followed the code of the author of a Data Science book, which is the following:

library(gapminder)

by_country <- gapminder %>% 
  group_by(country, continent) %>% 
  nest()

In that way the gapminder dataset is nested. There's only 1 row for each country and all data regarding that country is the the column 'data'.

Then I created a function for mapping all the cells of the nested data frame. Calling that function, each cell of the column 'data' will have a personal model.

country_model <- function(df) {
  lm(lifeExp ~ year, data = df)
}

In the end I tried to create a new column in the by_country dataset, by calling the previous function, but it says Error in eval(predvars, data, env) : object 'lifeExp' not found .

by_country <- by_country %>%
  mutate(model = map(data,country_model))

lifeExp is a column inside every cell of data, but it says it didn't find that. Honestly I don't understand where the problem is, can anybody help me, please?

I encountered this error with a similar set of package versions, and after updating dplyr, tidyr, and purrr the code ran successfully.

Showing truncated output from sessionInfo() for my session after loading only tidyverse, modelr, and gapminder.

Good luck!

other attached packages: [1] gapminder_0.3.0 modelr_0.1.2 forcats_0.3.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.2 readr_1.1.1 tidyr_1.0.0
[9] tibble_2.1.3 ggplot2_3.2.0 tidyverse_1.2.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM