【问题标题】:Replace all NAs with -1 in r with dplyr用 dplyr 将 r 中的所有 NA 替换为 -1
【发布时间】:2018-10-10 21:45:42
【问题描述】:

我目前正在使用 R 中的 tidyverse。在使用鼠标估算 NA 之后,由于一开始它们的填充量很少(我相信),一些列仍然具有 NA。作为最后的检查,我想用 -1 替换所有剩余的 NA。它通常只发生在单个列中,具体取决于数据集。长话短说,我在多个地点都在做同样的过程,有时 Col1 在 A 区的人口非常多,但在 B 区却很糟糕。

目前我正在做以下事情。

    Clean.df <- df %>% mutate(
              coalesce(Col1 ,-1),
              coalesce(Col2, -1),
              ....)

我正在为 31 列这样做,这让我认为一定有更简单的方法。我尝试阅读合并文档并尝试将其替换为数据框的名称,但没有成功。

感谢您的洞察力。

【问题讨论】:

  • 您专门要求dplyr 回答。这有时会受到限制。 “base R”解决方案通常简洁且完全可以理解,例如:df[is.na(df)] &lt;- -1

标签: r dplyr r-mice


【解决方案1】:

由于您没有提供任何数据,我使用示例数据框来展示如何将数据框中的每个 NA 替换为给定值 (-1):

library(tidyverse)

# creating example dataset
example_df <- ggplot2::msleep

# looking at NAs
example_df
#> # A tibble: 83 x 11
#>    name  genus vore  order conservation sleep_total sleep_rem sleep_cycle
#>    <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl>
#>  1 Chee~ Acin~ carni Carn~ lc                  12.1      NA        NA    
#>  2 Owl ~ Aotus omni  Prim~ <NA>                17         1.8      NA    
#>  3 Moun~ Aplo~ herbi Rode~ nt                  14.4       2.4      NA    
#>  4 Grea~ Blar~ omni  Sori~ lc                  14.9       2.3       0.133
#>  5 Cow   Bos   herbi Arti~ domesticated         4         0.7       0.667
#>  6 Thre~ Brad~ herbi Pilo~ <NA>                14.4       2.2       0.767
#>  7 Nort~ Call~ carni Carn~ vu                   8.7       1.4       0.383
#>  8 Vesp~ Calo~ <NA>  Rode~ <NA>                 7        NA        NA    
#>  9 Dog   Canis carni Carn~ domesticated        10.1       2.9       0.333
#> 10 Roe ~ Capr~ herbi Arti~ lc                   3        NA        NA    
#> # ... with 73 more rows, and 3 more variables: awake <dbl>, brainwt <dbl>,
#> #   bodywt <dbl>

# replacing NAs with -1
purrr::map_dfr(.x = example_df,
           .f = ~ tidyr::replace_na(data = ., -1))
#> # A tibble: 83 x 11
#>    name  genus vore  order conservation sleep_total sleep_rem sleep_cycle
#>    <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl>
#>  1 Chee~ Acin~ carni Carn~ lc                  12.1      -1        -1    
#>  2 Owl ~ Aotus omni  Prim~ -1                  17         1.8      -1    
#>  3 Moun~ Aplo~ herbi Rode~ nt                  14.4       2.4      -1    
#>  4 Grea~ Blar~ omni  Sori~ lc                  14.9       2.3       0.133
#>  5 Cow   Bos   herbi Arti~ domesticated         4         0.7       0.667
#>  6 Thre~ Brad~ herbi Pilo~ -1                  14.4       2.2       0.767
#>  7 Nort~ Call~ carni Carn~ vu                   8.7       1.4       0.383
#>  8 Vesp~ Calo~ -1    Rode~ -1                   7        -1        -1    
#>  9 Dog   Canis carni Carn~ domesticated        10.1       2.9       0.333
#> 10 Roe ~ Capr~ herbi Arti~ lc                   3        -1        -1    
#> # ... with 73 more rows, and 3 more variables: awake <dbl>, brainwt <dbl>,
#> #   bodywt <dbl>

reprex package (v0.2.1) 于 2018 年 10 月 10 日创建

【讨论】:

  • 建议/请求(这是个人偏好,我知道):尤其是在提问者可能是 R 新手的回答中,当您只需要两个25个依赖包。这不是一个硬性规定,但加载 25 Vice 2(嗯,2 加上它们的静默依赖)可能会有很大的不同,特别是如果(像我一样)其中许多实际上没有安装。我真的很喜欢你用双冒号指定它们,例如,purrr::map_dfr:这是一种类似于 python 的机制,用于保持命名空间显式。不错。
【解决方案2】:

Indrajeet 的答案是纯 dplyr 的替代方案。使用 Indrajeet 推荐的ggplot2::msleep

library(dplyr)
ggplot2::msleep %>%
  mutate_at(vars(sleep_rem, sleep_cycle), ~ if_else(is.na(.), -1, .))
# # A tibble: 83 x 11
#    name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
#    <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
#  1 Chee~ Acin~ carni Carn~ lc                  12.1      -1        -1      11.9
#  2 Owl ~ Aotus omni  Prim~ <NA>                17         1.8      -1       7  
#  3 Moun~ Aplo~ herbi Rode~ nt                  14.4       2.4      -1       9.6
#  4 Grea~ Blar~ omni  Sori~ lc                  14.9       2.3       0.133   9.1
#  5 Cow   Bos   herbi Arti~ domesticated         4         0.7       0.667  20  
#  6 Thre~ Brad~ herbi Pilo~ <NA>                14.4       2.2       0.767   9.6
#  7 Nort~ Call~ carni Carn~ vu                   8.7       1.4       0.383  15.3
#  8 Vesp~ Calo~ <NA>  Rode~ <NA>                 7        -1        -1      17  
#  9 Dog   Canis carni Carn~ domesticated        10.1       2.9       0.333  13.9
# 10 Roe ~ Capr~ herbi Arti~ lc                   3        -1        -1      21  
# # ... with 73 more rows, and 2 more variables: brainwt <dbl>, bodywt <dbl>

如果您想在所有列(numericcharacter)上使用核选项,请使用:

ggplot2::msleep %>%
  mutate_all(~ ifelse(is.na(.), -1, .))
# # A tibble: 83 x 11
#    name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
#    <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
#  1 Chee~ Acin~ carni Carn~ lc                  12.1      -1        -1      11.9
#  2 Owl ~ Aotus omni  Prim~ -1                  17         1.8      -1       7  
#  3 Moun~ Aplo~ herbi Rode~ nt                  14.4       2.4      -1       9.6
#  4 Grea~ Blar~ omni  Sori~ lc                  14.9       2.3       0.133   9.1
#  5 Cow   Bos   herbi Arti~ domesticated         4         0.7       0.667  20  
#  6 Thre~ Brad~ herbi Pilo~ -1                  14.4       2.2       0.767   9.6
#  7 Nort~ Call~ carni Carn~ vu                   8.7       1.4       0.383  15.3
#  8 Vesp~ Calo~ -1    Rode~ -1                   7        -1        -1      17  
#  9 Dog   Canis carni Carn~ domesticated        10.1       2.9       0.333  13.9
# 10 Roe ~ Capr~ herbi Arti~ lc                   3        -1        -1      21  
# # ... with 73 more rows, and 2 more variables: brainwt <dbl>, bodywt <dbl>

请注意,我不再使用dplyr::if_else,因为该函数需要通用(或不了解)不同类型。由于base::ifelse 会愉快地/默默地(/sloppily?)转换,我们很好。

【讨论】:

  • voreconservation等列中仍有NAs。
  • 当然有!我正在演示基本概念,假设 OP 可以适应他们自己的需求。替换为mutate_all(~ ifelse(is.na(.), -1, .)) 作为核选项可以正常工作;没有更多代码就不能使用dplyr::if_else,因为列的类型多种多样。
猜你喜欢
  • 2020-11-19
  • 2018-06-12
  • 1970-01-01
  • 2021-08-30
  • 2011-11-08
  • 2023-04-02
  • 2021-11-26
相关资源
最近更新 更多