如何使用 tidyr::replace_na 替换数据帧中的所有 NA? [英] How to replace all NA in a dataframe using tidyr::replace_na?

查看:20
本文介绍了如何使用 tidyr::replace_na 替换数据帧中的所有 NA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 0 填充数据中的所有 NA.有谁知道如何使用 tidyr 的 replace_na 做到这一点?从文档中,我们可以轻松地将不同列中的 NA 替换为不同的值.但是如何用某个值替换所有这些呢?我有很多列...

I'm trying to fill all NAs in my data with 0's. Does anyone know how to do that using replace_na from tidyr? From documentation, we can easily replace NA's in different columns with different values. But how to replace all of them with some value? I have many columns...

以 mtcars 数据集为例:

Using mtcars dataset as an example:

mtcars [sample(1:nrow(mtcars), 4), sample(1:ncol(mtcars), 4)]<- NA
mtcars %>% replace_na( ??? )

推荐答案

如果 replace_na 不是强制性要求,以下代码将起作用:

If replace_na is not a mandatory requirement, following code will work:

mtcars %>% replace(is.na(.), 0)

参考问题:https://stackoverflow.com/a/45574804/8382207

这篇关于如何使用 tidyr::replace_na 替换数据帧中的所有 NA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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