【问题标题】:How to replace NA with set of values如何用一组值替换 NA
【发布时间】:2020-02-14 07:34:19
【问题描述】:

我有以下数据框:

library(dplyr)
library(tibble)


df <- tibble(
  source = c("a", "b", "c", "d", "e"),
  score = c(10, 5, NA, 3, NA ) ) 


df

看起来像这样:

# A tibble: 5 x 2
  source score
  <chr>  <dbl>
1 a         10 . # current max value
2 b          5
3 c         NA
4 d          3
5 e         NA

我想要做的是将分数列中的NA 替换为现有max + n 以上的值。其中n的范围从1到df的总行数

导致这个(手工编码):

  source score
  a         10
  b          5
  c         11 # obtained from 10 + 1
  d          3
  e         12 #  obtained from 10 + 2

我怎样才能做到这一点?

【问题讨论】:

    标签: r if-statement dplyr replace tibble


    【解决方案1】:

    另一种选择:

    transform(df, score = pmin(max(score, na.rm = TRUE) + 
                          cumsum(is.na(score)), score, na.rm = TRUE))
    
    #  source score
    #1      a    10
    #2      b     5
    #3      c    11
    #4      d     3
    #5      e    12
    

    如果你想在dplyr这样做

    library(dplyr)
    df %>% mutate(score = pmin(max(score, na.rm = TRUE) + 
                          cumsum(is.na(score)), score, na.rm = TRUE))
    

    【讨论】:

      【解决方案2】:

      基础 R 解决方案

      df$score[is.na(df$score)] <- seq(which(is.na(df$score))) + max(df$score,na.rm = TRUE)
      

      这样

      > df
      # A tibble: 5 x 2
        source score
        <chr>  <dbl>
      1 a         10
      2 b          5
      3 c         11
      4 d          3
      5 e         12
      

      【讨论】:

      【解决方案3】:

      这是dplyr 方法,

      df %>% 
       mutate(score = replace(score, 
                             is.na(score), 
                             (max(score, na.rm = TRUE) + (cumsum(is.na(score))))[is.na(score)])
                             )
      

      给出,

      # A tibble: 5 x 2
        source score
        <chr>  <dbl>
      1 a         10
      2 b          5
      3 c         11
      4 d          3
      5 e         12
      

      【讨论】:

        【解决方案4】:

        dplyr:

        library(dplyr)
        
        df %>%
          mutate_at("score", ~ ifelse(is.na(.), max(., na.rm = TRUE) + cumsum(is.na(.)), .))
        

        结果:

        # A tibble: 5 x 2
          source score
          <chr>  <dbl>
        1 a         10
        2 b          5
        3 c         11
        4 d          3
        5 e         12
        

        【讨论】:

          【解决方案5】:

          dplyr 解决方案。

          df %>%
            mutate(na_count = cumsum(is.na(score)),
                   score = ifelse(is.na(score), max(score, na.rm = TRUE) + na_count, score)) %>%
            select(-na_count)
          ## A tibble: 5 x 2
          #  source score
          #  <chr>  <dbl>
          #1 a         10
          #2 b          5
          #3 c         11
          #4 d          3
          #5 e         12
          

          【讨论】:

            【解决方案6】:

            另一个,和ThomasIsCoding的方案很相似:

            > df$score[is.na(df$score)]<-max(df$score, na.rm=T)+(1:sum(is.na(df$score)))
            > df
            # A tibble: 5 x 2
              source score
              <chr>  <dbl>
            1 a         10
            2 b          5
            3 c         11
            4 d          3
            5 e         12
            

            【讨论】:

              【解决方案7】:

              与基本的 R 解决方案相比,不是很优雅,但仍然可行:

              library(data.table)
              setDT(df)
              
              max.score = df[, max(score, na.rm = TRUE)]
              df[is.na(score), score :=(1:.N) + max.score]
              

              或者在一行中,但有点慢:

              df[is.na(score), score := (1:.N) + df[, max(score, na.rm = TRUE)]]
              df
                 source score
              1:      a    10
              2:      b     5
              3:      c    11
              4:      d     3
              5:      e    12
              

              【讨论】:

                猜你喜欢
                • 2014-06-28
                • 2019-11-26
                • 2022-07-15
                • 1970-01-01
                • 1970-01-01
                • 2012-03-08
                • 2016-10-03
                • 2015-04-26
                相关资源
                最近更新 更多