微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据多个条件用预先存在的值替换 NA 值

如何解决根据多个条件用预先存在的值替换 NA 值

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

       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

根据发货公司的说法,当其中一个 Box 列的值为 NA 时,表示该物品已经装在一个盒子中并且正在按原样发货。所以,我只需要用 Item_Height 替换丢失的 Box_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

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

如果有人对如何正确执行此操作或我哪里出错有任何建议,我将不胜感激。

解决方法

尝试使用 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 短的向量,这是未子集的。

,

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

library(tidyverse)

df <- tibble::tribble(
  ~Box_Height,~Box_Length,~Box_Width,~Item_Height,~Item_Length,~Item_Width,NA,74,4,42,6,22,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
,

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。