对于选定的列,如何使用 sparklyr 用列平均值估算缺失值?

How to impute missing value with column mean using sparklyr, for selected columns?

对于 sparklyr 中的 Spark 数据帧,我知道 NA 可以使用 na.replace(number) 由固定数字估算,我也知道我可以对硬编码列执行 na.replace(x=something)

现在我有一个包含列名的向量,我想用平均值来估算缺失值。我该怎么做才能为这些列中的所有缺失值插入均值?

我研究了 spark_apply 以在其上应用 mice,但还没有找到解决方案。

谢谢!

您可以使用 Imputer。假设数据如下所示:

df <- copy_to(sc, tibble(id=1:3, x=c(1, NA, 3), y=c(NA, 2, -1)))

转换器需要输入和输出列列表:

input_cols <- c("x", "y")
output_cols <- paste0(input_cols, "_imp")

可按如下方式应用:

df %>% 
  ft_imputer(input_cols=input_cols, output_cols=output_cols, strategy="mean")
# Source:   table<sparklyr_tmp_73a32e74369c> [?? x 5]
# Database: spark_connection
     id     x     y x_imp y_imp
  <int> <dbl> <dbl> <dbl> <dbl>
1     1     1   NaN     1   0.5
2     2   NaN     2     2   2  
3     3     3    -1     3  -1