Applying Random Operations to Groups of Columns

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!

Save the output of sample(nrow(my_data),25) in a variable and use that to subset my_data.

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)

Rows25 <- sample(nrow(my_data),25)

my_data$var_1[Rows25]<-NA
my_data$var_2[Rows25]<-NA
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.