替换dplyr链的所有列中的NA [英] Replace NA in all columns of a dplyr chain

查看:123
本文介绍了替换dplyr链的所有列中的NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将dplyr链中的NA替换为问题进入解决方案

dt %.% group_by(a) %.% mutate(b = ifelse(is.na(b), mean(b, na.rm = T), b))

和dplyr。我想用dplyr链估算所有列。没有分组的单个列,而是我希望所有数字列都用诸如列均值之类的方式替换所有NA。

with dplyr. I want to impute all colums with dplyr chain. There is no single column to group by, rather I want all numeric columns to have all NAs replaced by the means such as column means.

什么是最

推荐答案

我们可以使用 mutate_all ifelse

dt %>%
   group_by(a) %>% 
   mutate_all(funs(ifelse(is.na(.), mean(., na.rm = TRUE), .)))






如果我们想要一个紧凑的选项,请使用 na.aggregate 来自 zoo ,默认为 替换 NA 平均值


If we want a compact option, then use the na.aggregate from zoo which by default replace NA values with mean

dt %>% 
   group_by(a) %>% 
   mutate_all(zoo::na.aggregate)






如果我们没有分组变量,则删除 group_by 并使用 mutate_if (请谨慎对待ut有一些非数字列)


If we don't have a grouping variable, then remove the group_by and use mutate_if (just to be cautious about having some non-numeric column)

dt %>%
   mutate_if(is.numeric, zoo::na.aggregate)

如果所有列都是数字,即使

If all the columns are numeric, even

zoo::na.aggregate(dt)



data



data

set.seed(42)
dt <- data.frame(a = rep(letters[1:3], each = 3),
                 b= sample(c(NA, 1:5), 9, replace = TRUE), 
                 c = sample(c(NA, 1:3), 9, replace = TRUE))

这篇关于替换dplyr链的所有列中的NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆