【问题标题】:Delete incomplete cases in nested dataframe using map function from purrr使用 purrr 中的 map 函数删除嵌套数据框中的不完整案例
【发布时间】:2021-01-17 14:11:08
【问题描述】:

我想从嵌套 tibble 的每个数据框中删除不完整的案例。 我确实尝试使用 map 函数(purrr 包),但收到以下错误消息“parent.env(x) 中的错误:参数不是环境”。我不明白这是什么问题。

这是一个可复制的例子。

library(tidyverse)

gapminder_orig <- read.csv("https://raw.githubusercontent.com/swcarpentry/r-novice-gapminder/gh-pages/_episodes_rmd/data/gapminder-FiveYearData.csv")

gapminder_orig <- gapminder_orig %>%
  dplyr::select(continent, country, year, pop, lifeExp, gdpPercap)

data_with_NA<-map_df(gapminder_orig[,4:6], function(x) {x[sample(c(TRUE, NA), prob = c(0.8, 0.2), size = length(x), replace = TRUE)]})

gapminder_orig_with_NA<-gapminder_orig %>%
  mutate(pop=data_with_NA$pop, lifeExp=data_with_NA$lifeExp, gdpPercap=data_with_NA$gdpPercap)

gapminder_nested <- gapminder_orig_with_NA %>% 
  mutate(dummy_var= sample(1:3, nrow(gapminder_orig_with_NA), replace=TRUE)) %>%
  group_by(continent) %>% 
  nest() %>%
  add_column(Type=c("Full", "Full", "Subset","Subset","Subset")) %>%
  add_column(Sector=c("Agriculture", "Banking", "Agriculture", "Banking", "Agriculture"))
gapminder_nested

remove_NA<-function(x) {
  y <- x[complete.cases(x),]
  return(y)
}

remove_NAz<-function(x, z) {
y <- x[complete.cases(x),]
return(y)
}

test<-gapminder_nested  %>%
  #mutate(data2 = map(.x=data, .f=filter(complete.cases(.x))))  #Does not work
  #mutate(data2 = map(.x=data, .f=na.omit)) #Does not work
  #mutate(data2 = map(data, ~ map_dfc(., na.omit))) #Does not work
  #mutate(data2 = map(data, function(.x) remove_NA(.x))) #Does not work
  mutate(data2= map2(data, Type, function(.x, .z) remove_NAz(.x, .z))) #Work but not elegant

知道调用 map 函数出了什么问题吗? 为什么它适用于 map2?

谢谢!

【问题讨论】:

    标签: r tidyverse purrr


    【解决方案1】:

    据我所知,至少您的第二种方法效果很好。还要使第一种方法起作用,请使用.f = ~ filter(.x, complete.cases(.x))

    这两种方法给我的结果与您使用 map2 的最终方法相同

    library(dplyr)
    library(purrr)
    library(tidyr)
    library(tibble)
    
    set.seed(42)
    gapminder_orig <- gapminder::gapminder
    
    gapminder_orig <- gapminder_orig %>%
      dplyr::select(continent, country, year, pop, lifeExp, gdpPercap)
    
    data_with_NA<-map_df(gapminder_orig[,4:6], function(x) {x[sample(c(TRUE, NA), prob = c(0.8, 0.2), size = length(x), replace = TRUE)]})
    
    gapminder_orig_with_NA<-gapminder_orig %>%
      mutate(pop=data_with_NA$pop, lifeExp=data_with_NA$lifeExp, gdpPercap=data_with_NA$gdpPercap)
    
    gapminder_nested <- gapminder_orig_with_NA %>% 
      mutate(dummy_var= sample(1:3, nrow(gapminder_orig_with_NA), replace=TRUE)) %>%
      group_by(continent) %>% 
      nest() %>%
      add_column(Type=c("Full", "Full", "Subset","Subset","Subset")) %>%
      add_column(Sector=c("Agriculture", "Banking", "Agriculture", "Banking", "Agriculture"))
    
    remove_NAz<-function(x, z) {
      y <- x[complete.cases(x),]
      return(y)
    }
    
    gapminder_nested  %>%
      mutate(data2 = map(data, ~ filter(.x, complete.cases(.x))))
    #> # A tibble: 5 x 5
    #> # Groups:   continent [5]
    #>   continent data               Type   Sector      data2             
    #>   <fct>     <list>             <chr>  <chr>       <list>            
    #> 1 Asia      <tibble [396 x 6]> Full   Agriculture <tibble [185 x 6]>
    #> 2 Europe    <tibble [360 x 6]> Full   Banking     <tibble [195 x 6]>
    #> 3 Africa    <tibble [624 x 6]> Subset Agriculture <tibble [311 x 6]>
    #> 4 Americas  <tibble [300 x 6]> Subset Banking     <tibble [150 x 6]>
    #> 5 Oceania   <tibble [24 x 6]>  Subset Agriculture <tibble [10 x 6]>
    
    gapminder_nested  %>%
      mutate(data2 = map(.x=data, .f=na.omit)) 
    #> # A tibble: 5 x 5
    #> # Groups:   continent [5]
    #>   continent data               Type   Sector      data2             
    #>   <fct>     <list>             <chr>  <chr>       <list>            
    #> 1 Asia      <tibble [396 x 6]> Full   Agriculture <tibble [185 x 6]>
    #> 2 Europe    <tibble [360 x 6]> Full   Banking     <tibble [195 x 6]>
    #> 3 Africa    <tibble [624 x 6]> Subset Agriculture <tibble [311 x 6]>
    #> 4 Americas  <tibble [300 x 6]> Subset Banking     <tibble [150 x 6]>
    #> 5 Oceania   <tibble [24 x 6]>  Subset Agriculture <tibble [10 x 6]>
    
    gapminder_nested %>% 
      mutate(data2= map2(data, Type, function(.x, .z) remove_NAz(.x, .z)))
    #> # A tibble: 5 x 5
    #> # Groups:   continent [5]
    #>   continent data               Type   Sector      data2             
    #>   <fct>     <list>             <chr>  <chr>       <list>            
    #> 1 Asia      <tibble [396 x 6]> Full   Agriculture <tibble [185 x 6]>
    #> 2 Europe    <tibble [360 x 6]> Full   Banking     <tibble [195 x 6]>
    #> 3 Africa    <tibble [624 x 6]> Subset Agriculture <tibble [311 x 6]>
    #> 4 Americas  <tibble [300 x 6]> Subset Banking     <tibble [150 x 6]>
    #> 5 Oceania   <tibble [24 x 6]>  Subset Agriculture <tibble [10 x 6]>
    

    【讨论】:

    • 感谢您的回答斯特凡。我重新安装了 R,并从一个干净的环境重新开始,它也对我有用,包括使用您的更正的第一种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 2014-04-16
    相关资源
    最近更新 更多