我正在处理以下数据。它与物品的尺寸和运送它们的盒子有关。
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 值时,这意味着该物品已经装在一个箱子中并且正在按原样发货。因此,我只需要用 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
这有点令人困惑,因为这就是我要替换的内容。
如果有人对如何正确执行此操作或我哪里出错有任何建议,我将非常感谢您的帮助。