I am working with the R programming language.
Suppose I have the following dataswt:
id = 1:100
var_1 = rnorm(100,100,100)
var_2 = rnorm(100,100,100)
var_3 = rnorm(100,100,100)
var_4 = rnorm(100,100,100)
my_data = data.frame(id, var_1, var_2, var_3, var_4)
- I want to randomly replace 25 of the SAME entries in var_1 and var_2 with NA
- I want to randomly replace 20 of the SAME entries in var_3 and var_4 with NA.
I found the following code that can replace entries in a column with NA:
my_data$var_1[sample(nrow(my_data),25)]<-NA
my_data$var_2[sample(nrow(my_data),25)]<-NA
my_data$var_3[sample(nrow(my_data),20)]<-NA
my_data$var_4[sample(nrow(my_data),20)]<-NA
But is there someway to ensure that the same entries in var_1 and var_2 are replaced with NA, and the same entries in var_3 and var_4 are replace with NA?
Can someone please show me how to do this?
Thanks!