Reputation: 333
I'm trying to replace the values of a column with NA with the mean of the column.
I already tried the following solutions:
titanic3$age[which(is.na(titanic3$age))] <- mean(titanic3$age, use =
"complete.obs")
titanic3$age[which(is.na(titanic3$age))] <- mean(titanic3[,age], use =
"complete.obs")
titanic3$age[which(is.na(titanic3$age))] <- mean(titanic3[[age]], use =
"complete.obs")
But it didn´t work. Any ideas?
Upvotes: 0
Views: 128
Reputation: 887158
We can do this easily with na.aggregate
library(zoo)
titanic3$age <- na.aggregate(titanic3$age)
By default, the FUN
would be mean
to replace the NA
values with the mean
of that column
Upvotes: 2
Reputation: 47320
Try this :
titanic3$age[is.na(titanic3$age)] <- mean(titanic3$age, na.rm = T)
Upvotes: 0