I have a column with a lot of missing values. I want to randomly replace some of these missing values (not all!) with a number, and others with another number.
Example: a column with 10000 values, some of which are missing. From those missing values, randomly select 50 and change from NA to 1. Also, randomly select another 30 missing values and changes from NA to 5.
what I've tried:
rows<- test1[test1== NA]
rows_to_replace<-sample (rows, 30, REPLACE = FALSE)
test1[rows_to_replace,]<-5
But I cant get it to work.
Some sample data
test1<-sample(c(0.5:10, NA), 10000, replace = T)