How to exclude variable E observations in a matrix in R?

Asked

Viewed 120 times

2

The situation is as follows: I have a matrix with 271 observations and 14 variables in R. Need to randomly exclude a certain number of crossings and variables, maintaining the order of the remaining crossings and adding NA’s in the excluded crossings.

At present, I can randomly delete and replace the observations ("lines" or "Rows"), using the following code:

X100amostra <- X100[sample(1:nrow(X100), 50,
   replace=TRUE),]

where "50" is the number of random exclusions.

Still, I can make exclusions in the form of vectors. I would like to do in the form of matrix.

Any suggestions?

1 answer

1

See if that’s what you want:

matriz<- matrix(1:3794,ncol=14) # Cria uma matriz qualquer 271x14
matriz[sample(1:length(matriz),50,replace=FALSE)]<-NA # Substitui 50 valores por NA aleatoriamente

Browser other questions tagged

You are not signed in. Login or sign up in order to post.