【问题标题】:Row replacement without NA's in RR中没有NA的行替换
【发布时间】:2018-06-26 15:10:22
【问题描述】:

我有这个数据框

    # of int.   int.   not.int.  ID  group   odd      even
2      24      85.15    113.34   2   thc1    NA       486.66
3      33      134.94   158.17   3   thc2    465.06   NA
4      12      47.60    62.73    4   thc3    NA       537.27
1      50      218.41   372.16   1   veh     381.59   NA
5      44      176.81   268.92   5   veh     NA       331.08

如何替换 'int.' 中的数字。 '偶数'中的数字不添加 NA's 和'奇数'和'not.int'相同?所以它看起来像这样。

 # of int.   int.     not.int.  ID  group   odd      even
2      24    486.66   113.34    2   thc1    NA       486.66
3      33    134.94   465.06    3   thc2    465.06   NA
4      12    537.27   62.73     4   thc3    NA       537.27
1      50    218.41   381.59    1   veh     381.59   NA
5      44    331.08   268.92    5   veh     NA       331.08

【问题讨论】:

    标签: r dataframe na


    【解决方案1】:

    一种选择是在base R 中使用Map 来获取相应的列,然后进行赋值

    df1[2:3] <- Map(function(x, y) {
                  i1 <- !is.na(y)
                  x[i1] <- y[i1]
                   x}, df1[c('int.', 'not.int.')], df1[c('even', 'odd')])
    
    df1
    #  # of int.   int. not.int. ID group    odd   even
    #2        24 486.66   113.34  2  thc1     NA 486.66
    #3        33 134.94   465.06  3  thc2 465.06     NA
    #4        12 537.27    62.73  4  thc3     NA 537.27
    #1        50 218.41   381.59  1   veh 381.59     NA
    #5        44 331.08   268.92  5   veh     NA 331.08
    

    数据

     df1 <- structure(list(`# of int.` = c(24L, 33L, 12L, 50L, 44L), int. = c(85.15, 
    134.94, 47.6, 218.41, 176.81), not.int. = c(113.34, 158.17, 62.73, 
    372.16, 268.92), ID = c(2L, 3L, 4L, 1L, 5L), group = c("thc1", 
    "thc2", "thc3", "veh", "veh"), odd = c(NA, 465.06, NA, 381.59, 
    NA), even = c(486.66, NA, 537.27, NA, 331.08)), .Names = c("# of int.", 
    "int.", "not.int.", "ID", "group", "odd", "even"), 
    class = "data.frame", row.names = c("2", 
    "3", "4", "1", "5"))
    

    【讨论】:

      【解决方案2】:

      使用dplyr 包中的mutateifelse

      library(dplyr)
      
      df %>%
       mutate(int. = ifelse(is.na(even), int., even),
              not.int. = ifelse(is.na(odd), not.int., odd))
      

      【讨论】:

        【解决方案3】:

        这是基础 R 中的一个选项,带有 ifelseis.na

        dat$int <- with(dat, ifelse(!is.na(even), even, int))
        dat$not.int <- with(dat, ifelse(!is.na(odd), odd, not.int))
        

        数据

        dat <- read.table(text = "    '# of int'   int   'not.int'  ID  group   odd      even
        2      24      85.15    113.34   2   thc1    NA       486.66
        3      33      134.94   158.17   3   thc2    465.06   NA
        4      12      47.60    62.73    4   thc3    NA       537.27
        1      50      218.41   372.16   1   veh     381.59   NA
        5      44      176.81   268.92   5   veh     NA       331.08",
                          header = TRUE, stringsAsFactors = FALSE)
        

        【讨论】:

          【解决方案4】:
          x <- df1[c("even","odd")]
          df1[c("int.","not.int.")][!is.na(x)] <- x[!is.na(x)]
          
          # #   of int.   int. not.int. ID group    odd   even
          # 2        24 486.66   113.34  2  thc1     NA 486.66
          # 3        33 134.94   465.06  3  thc2 465.06     NA
          # 4        12 537.27    62.73  4  thc3     NA 537.27
          # 1        50 218.41   381.59  1   veh 381.59     NA
          # 5        44 331.08   268.92  5   veh     NA 331.08
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-01-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-13
            • 2019-10-11
            • 2020-04-23
            • 2021-11-26
            相关资源
            最近更新 更多