I have a vector with 10000+ entries, approximately 4000 of them are zeros. I want to add random values to those entries which are NOT zero. I know how to find out which indices are zero, but how can I make the addition?. For example consider this vector with only 10 indices
v <- c(0, 0.1, 0, 0, 5, 0,3, 0.8, 0, 4)
v_notzero <- which(v != 0) #extracting the non-zero entries
[1] 2 5 7 8 10
length(v_notzero)
[1] 5
now I know both the indices where v is not zero as well as the amount of entries which are non-zero. So I would like to create a random vector, which is filled with zeros at the same indices as v, and filled with random variables at v_notzero. How can I do this?
rand <- rnorm(5)