运行 如果在多个条件下,没有错误,但数据没有改变

Run if on multiple conditions, no error, but data doesn't change

我尝试用条件中位数来估算缺失数据,没有错误,但数据没有改变。为什么以及如何让它发挥作用?

housing = read.csv('housingsample22.csv')
if (housing$Car == 0 && housing$Tunit == 1) {
    housing$Car = median(housing$Car)
}
if (housing$Landsize == 0 && housing$Tunit != 1) { 
   housing$Landsize = median(housing$Landsize)
}
if (housing$BuildingArea == 0 && housing$TTHouse == 1) {
    housing$BuildingArea = median(housing$BuildingArea[housing$TTHouse == 1])
}

您的数据没有改变是有原因的。以第一个条件为例:

if (housing$Car == 0 && housing$Tunit == 1) 
   {housing$Car = median(housing$Car)}

您正在检查整列是否为零。它肯定只需要第一个元素来评估条件。

你可以这样做:

require(dplyr)
median_car = median(housing$Car)

housing = housing %>% 
  mutate(Car = if_else(Car == 0 & Tunit == 1,median_car,Car))

你可以将其他3个语句类似地写在一起。 让我知道这是否有效。