r4ds第20章中的purrr :: map和嵌套数据框错误

时间:2019-05-24 13:43:50

标签: r purrr

尝试浏览R4DS第20章的列表列部分时出现错误

# Required Libraries
library(tidyverse)
library(modelr)
library(gapminder)

# Created nested data frame 
# Each row is a group (one country through time)
by_country <- gapminder %>% 
  group_by(country,continent) %>% 
  nest()

# lm function for life expectancy and years
country_model <- function(df) {
  lm(lifeExp ~ year, data = df)
}

# Save the lm results back into the data frame
by_country <- by_country %>%
  mutate(model = map(.$data, country_model))

  

eval(predvars,data,env)中的错误:找不到对象'lifeExp'

> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United 
States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gapminder_0.3.0 modelr_0.1.4    forcats_0.3.0   stringr_1.4.0   dplyr_0.8.0.1   purrr_0.3.0     readr_1.3.1    
[8] tidyr_0.8.2     tibble_2.1.1    ggplot2_3.1.0   tidyverse_1.2.1

loaded via a namespace (and not attached):
[1] Rcpp_1.0.1       cellranger_1.1.0 pillar_1.3.1     compiler_3.5.0   plyr_1.8.4       tools_3.5.0      jsonlite_1.6    
[8] lubridate_1.7.4  gtable_0.2.0     nlme_3.1-137     lattice_0.20-38  pkgconfig_2.0.2  rlang_0.3.4      cli_1.1.0       
[15] rstudioapi_0.9.0 yaml_2.2.0       haven_2.0.0      withr_2.1.2      xml2_1.2.0       httr_1.4.0       generics_0.0.2  
[22] hms_0.4.2        grid_3.5.0       tidyselect_0.2.5 glue_1.3.1       R6_2.4.0         readxl_1.2.0     magrittr_1.5    
[29] backports_1.1.3  scales_1.0.0     rvest_0.3.2      assertthat_0.2.1 colorspace_1.4-0 stringi_1.4.3    lazyeval_0.2.2  
[36] munsell_0.5.0    broom_0.5.1      crayon_1.3.4    

0 个答案:

没有答案