简体   繁体   English

使用 mutate 将 tibble 中的 NaN 替换为 NA

[英]replace NaN to NA in tibble using mutate

Convert NaN to NA in tibble using mutate使用 mutate 在 tibble 中将 NaN 转换为 NA

tbl = tibble(x = c(NaN, NaN, 2, 3, 4, NA, NA, NaN, 5))

This will change all NaN s to NA in all numeric columns.这会将所有数字列中的所有NaN更改为NA

tbl %>% mutate(across(where(is.numeric), \(x) ifelse(is.nan(x), NA, x)))
# # A tibble: 9 × 1
#       x
#   <dbl>
# 1    NA
# 2    NA
# 3     2
# 4     3
# 5     4
# 6    NA
# 7    NA
# 8    NA
# 9     5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM