r - 使用 dplyr、cross()、where() 和 if_else() 函数将 NA 替换为数字
问题描述
这是我的datafrmae:
library(mlbench)
data(BreastCancer)
df_1<-BreastCancer
我做了一些修改,并在“Bare.nuclei”列上出现了“Na”:
df_1<-df_1 %>% mutate(across(where(~is.factor(.x)),as.numeric))
我正在学习如何使用across()
,where()
以及ifelse()
将这些 NA 替换为0
.
df_1 %>% mutate(across(where(~ is.numeric(.x)), if_else(is.na(.x,0,.x))))
我究竟做错了什么?
这里的想法是across
where
列是numerics
,如果在这些列上Nas
我将替换为111
,否则将替换为 mantain x。
解决方案
这可以通过tidyr::replace_na()
函数而不是if_else
.
df_1 %>% mutate(across(where(~ is.numeric(.x)), function(x){replace_na(x, 0)}))
推荐阅读
- docker - 在同一个应用程序中处理多个 docker-compose 文件
- c# - 在给定角度的椭圆上找到点,然后反转该过程
- reactjs - ReactJS:如何在没有 1 单击延迟的情况下刷新组件
- javascript - 如何返回 Array 对象中的最后一项
- sql - SQL 查询从一个表中提取与第二个表中的单列数据匹配的数据
- sql - 如何查找属于周末和工作日的项目计数
- scala - 在 Scala 中进行一系列验证的功能方法
- php - 最大执行时间 (max_execution_time) 0 唯一的解决方案是更改主题
- xcode10 - 资产文件夹中的图像未显示在主故事板上(Xcode 10)
- macos - 如何通过自制软件安装 gpg2?