【问题标题】:Replace NA values with preexisting value based on multiple conditions根据多个条件将 NA 值替换为预先存在的值
【发布时间】:2021-07-08 03:20:00
【问题描述】:

我正在处理以下数据。它与物品的尺寸和运送它们的盒子有关。

       Box_Height     Box_Length     Box_Width Item_Height Item_Length Item_Width
 1             NA             74             4          NA          NA         NA
 2             NA             42            NA           6          42          6
 3              6             NA            NA           6          22          6
 4              6             NA            NA           6          42          6
 5              6             NA            NA           6          42          6
 6             NA             NA            NA          NA          NA         NA

根据运输公司的说法,当其中一个箱子列具有 NA 值时,这意味着该物品已经装在箱子中并且正在按原样运输。因此,我只需要将缺少的 Box_Height 替换为 Item_Height。

为此,我编写了以下代码:

df$Box_Height[is.na(df$Box_Height) & !is.na(df$Item_Height)] <- df$Item_Height

我最终会尝试测试一行何时缺少框尺寸并且特定项目尺寸没有缺失,然后用该项目替换缺少的框尺寸维度。

我收到此错误:

Error in df$Box_Height[is.na(df$Box_Height) &  : 
  NAs are not allowed in subscripted assignments

这有点令人困惑,因为这是我要替换的内容。

如果有人对如何正确执行此操作或我哪里出错有任何建议,我将非常感谢您的帮助。

【问题讨论】:

    标签: r na missing-data dplyr across


    【解决方案1】:

    尝试使用ifelse() 应用相同的条件。

    df$Box_Height <- ifelse(is.na(df$Box_Height) & !is.na(df$Item_Height), df$Item_Height, df$Box_Height)
    

    ifelse() 函数要求您分别为条件为真和假的情况提供值,以确保向量长度匹配。用[df$Box_Height 进行子集化可能会产生一个比df$Item_Height 短的向量,而df$Item_Height 没有被子集化。

    【讨论】:

    • 感谢您的帮助!这就是我需要的。我发誓 R 语法会随机破坏我的大脑。
    【解决方案2】:

    我建议使用 tidyverse 语法。 并使用if_else 而不是ifelse

    library(tidyverse)
    
    df <- tibble::tribble(
      ~Box_Height, ~Box_Length, ~Box_Width, ~Item_Height, ~Item_Length, ~Item_Width,
      NA,         74,          4,           NA,         NA,          NA,
      NA,         42,         NA,           6,          42,          6,
      6,          NA,         NA,           6,          22,          6,
      6,          NA,         NA,           6,          42,          6,
      6,          NA,         NA,           6,          42,          6,
      NA,         NA,         NA,           NA,         NA,          NA
    )
    
    
    df %>%
      mutate(Item_Height = if_else(
        is.na(Box_Height) & !is.na(Item_Height),
        Item_Height,
        Box_Height
      ))
    #> # A tibble: 6 x 6
    #>   Box_Height Box_Length Box_Width Item_Height Item_Length Item_Width
    #>        <dbl>      <dbl>     <dbl>       <dbl>       <dbl>      <dbl>
    #> 1         NA         74         4          NA          NA         NA
    #> 2         NA         42        NA           6          42          6
    #> 3          6         NA        NA           6          22          6
    #> 4          6         NA        NA           6          42          6
    #> 5          6         NA        NA           6          42          6
    #> 6         NA         NA        NA          NA          NA         NA
    

    【讨论】:

      【解决方案3】:

      此语法将同时对框的所有属性(尺寸)执行所需的操作

      library(dplyr)
      df %>% mutate(across(starts_with("Box"), ~ ifelse(is.na(.x), 
                                                        get(str_replace(cur_column(), "Box", "Item")),
                                                        .x)))
      
      # A tibble: 6 x 6
        Box_Height Box_Length Box_Width Item_Height Item_Length Item_Width
             <dbl>      <dbl>     <dbl>       <dbl>       <dbl>      <dbl>
      1         NA         74         4          NA          NA         NA
      2          6         42         6           6          42          6
      3          6         22         6           6          22          6
      4          6         42         6           6          42          6
      5          6         42         6           6          42          6
      6         NA         NA        NA          NA          NA         NA
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-13
        • 1970-01-01
        • 1970-01-01
        • 2021-07-19
        • 2015-12-24
        • 2018-10-29
        相关资源
        最近更新 更多