This has been troubling me for long time now. I think I have seen the solution to this somewhere but can't recollect the way to solve this. Consider a vector x <- c(2, 3, 4, NA, 2, NA)
I want to replace NA values with random non-NA values from the vector. I am trying
ifelse(is.na(x), sample(x[!is.na(x)], sum(is.na(x))), x)
but this replaces with same non-NA value for all .
I want to replace NA values with random non-NA values from the vector. I am trying
ifelse(is.na(x), sample(x[!is.na(x)], sum(is.na(x))), x)
but this replaces with same non-NA value for all .