【问题标题】:Using replace_na with across in mutate在 mutate 中使用 replace_na 和 cross
【发布时间】:2020-09-19 15:09:38
【问题描述】:

我正在尝试替换列子集中的 NA,并且我想使用 tidyverse/dplyr 语法。 dplyr v1.0.2

在下文中,我只想在 ab,ac 列中将 NA 替换为 999,而不是在 ads 中替换

tbf <- tibble( ab = c(1,3,NA), ac = c(23,NA,33), d = c(22,22,NA), ads = c('ds', NA, "dwe"))
tbf %>% mutate(across(starts_with('a') & where(is.numeric)), ~replace_na(999))

似乎不起作用。

我也试过~replace_na(.x,999)。那也没用。

感谢任何帮助。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    fn(您使用的是replace_na)需要在across 内。然后,您还需要通过在replace_na 中插入. 来引用当前列。通过这种方式,您可以使用您在问题中提出的过滤(以“a”开头并带有数值的列),而不是此处专门使用列名的其他答案。

    tbf %>% mutate(across(starts_with('a') & where(is.numeric), ~replace_na(.,999)))
    

    【讨论】:

      【解决方案2】:

      数据

      tbf <- tibble( ab = c(1,3,NA), ac = c(23,NA,33), d = c(22,22,NA), ads = c('ds', NA, "dwe"))
      

      代码

      library(dplyr)
      library(tidyr)
      
      tbf %>% 
        mutate(
          across(ab:ac, ~replace_na(.x, 999))
        )
      

      输出

      #> # A tibble: 3 x 4
      #>      ab    ac     d ads  
      #>   <dbl> <dbl> <dbl> <chr>
      #> 1     1    23    22 ds   
      #> 2     3   999    22 NA 
      #> 3   999    33    NA dwe
      

      reprex package (v0.3.0) 于 2020 年 9 月 19 日创建

      【讨论】:

        【解决方案3】:

        试试这个:

        #Code
        tbf %>% mutate(across(ab:ac, ~ifelse(is.na(.),999,.)))
        

        输出:

        # A tibble: 3 x 4
             ab    ac     d ads  
          <dbl> <dbl> <dbl> <chr>
        1     1    23    22 ds   
        2     3   999    22 NA   
        3   999    33    NA dwe  
        

        【讨论】:

          【解决方案4】:

          请看看这是否有效。

          tbf %>% mutate(across(starts_with('a') & where(is.numeric), ~replace(., is.na(.), 999)))
          
          > tbf %>% mutate(across(starts_with('a') & where(is.numeric), ~replace(., is.na(.), 999)))
          # A tibble: 3 x 4
               ab    ac     d ads  
            <dbl> <dbl> <dbl> <chr>
          1     1    23    22 ds   
          2     3   999    22 NA   
          3   999    33    NA dwe  
          > 
          

          【讨论】:

          • replace_na 或 replace 都对我有用,但显然它对你有用。这是令人惊讶的。你能告诉我你用的是什么版本吗?
          • 我用的是dplyr 1.0.0版,你呢?
          • 它在干净的会话中运行良好。但是,我接受了 Juljo 的回答,但这个回答也有效。
          【解决方案5】:

          您也可以使用 tidyr 做到这一点。

          tbf %>% 
            mutate(across(starts_with('a') & where(is.numeric))) %>% 
            replace_na(list(ab = 999, ac = 999))
          
          
              # A tibble: 3 x 4
                   ab    ac     d ads  
                <dbl> <dbl> <dbl> <chr>
              1     1    23    22 ds   
              2     3   999    22 NA   
              3   999    33    NA dwe 
          

          【讨论】:

          • 这很接近,但我不想在 replace_na 中明确指定列名。这可能吗?
          • 此解决方案有效。但是,您在这里使用mutate(...) 呼叫做什么?好像什么都没做
          猜你喜欢
          • 2021-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-17
          • 1970-01-01
          • 1970-01-01
          • 2021-07-24
          • 1970-01-01
          相关资源
          最近更新 更多