I have a vector with the following format:
> test <- c("3", "5", "?", "7")
> test <- as.numeric(test)
> test
[1] 3 5 NA 7
Is it possible to specify what happens to missing values when calling as.numeric? I would like to use the mean, median, or any other suitable calculation of the other values instead.
答案 0 :(得分:1)
你要求的东西几乎不可能。如果要将值替换为平均值,则必须首先读取所有值。 但是,您可以在以后修复它:
test[is.na(test)] <- mean(test, na.rm=TRUE)