本文翻译自:How do I replace NA values with zeros in an R dataframe?
I have a data frame and some columns have NA
values. 我有一个数据框,有些列有NA
值。
How do I replace these NA
values with zeroes? 如何用零代替这些NA
值?
参考:https://stackoom.com/question/YFGW/如何在R数据帧中用零替换NA值
You can use replace()
您可以使用replace()
For example: 例如:
> x <- c(-1,0,1,0,NA,0,1,1)
> x1 <- replace(x,5,1)
> x1
[1] -1 0 1 0 1 0 1 1
> x1 <- replace(x,5,mean(x,na.rm=T))
> x1
[1] -1.00 0.00 1.00 0.00 0.29 0.00 1.00 1.00
如果我们在导出时尝试替换NA
,例如在写入csv时,则可以使用:
write.csv(data, "data.csv", na = "0")
dplyr example: dplyr示例:
library(dplyr)
df1 <- df1 %>%
mutate(myCol1 = if_else(is.na(myCol1), 0, myCol1))
Note: This works per selected column, if we need to do this for all column, see @reidjax 's answer using mutate_each . 注意:这适用于每个选定的列,如果我们需要对所有列执行此操作,请参阅@reidjax使用mutate_each的答案。
I know the question is already answered, but doing it this way might be more useful to some: 我知道这个问题已经回答了,但是这样做对某些人可能更有用:
Define this function: 定义此功能:
na.zero <- function (x) {
x[is.na(x)] <- 0
return(x)
}
Now whenever you need to convert NA's in a vector to zero's you can do: 现在,每当需要将向量中的NA转换为零时,都可以执行以下操作:
na.zero(some.vector)
More general approach of using replace()
in matrix or vector to replace NA
to 0
在矩阵或向量中使用replace()
将NA
替换为0
更通用方法
For example: 例如:
> x <- c(1,2,NA,NA,1,1)
> x1 <- replace(x,is.na(x),0)
> x1
[1] 1 2 0 0 1 1
This is also an alternative to using ifelse()
in dplyr
这也是在dplyr
使用ifelse()
的dplyr
df = data.frame(col = c(1,2,NA,NA,1,1))
df <- df %>%
mutate(col = replace(col,is.na(col),0))