2

I have a vector like that

objetosDisponibles <- c(1,2,3,4)

I choose random one with this

objetoAleatorio <- sample(objetosDisponibles,size = 1, replace = F)

Then, I delete the choosen element in the vector

objetosDisponibles <- objetosDisponibles[objetosDisponibles!=objetoAleatorio]

If I do this 4 times, I want to choose the elements ramdonly, when I used sample method at the vector with 1 element, it choose other diferent. You can probe this with this code:

cont <- 0
objetosDisponibles <- c(1,2,3,4)

while(cont < 4){

 objetoAleatorio <- sample(objetosDisponibles,size = 1, replace = F)
 print(objetoAleatorio)
 objetosDisponibles <-  objetosDisponibles[objetosDisponibles!=objetoAleatorio]
 print(objetosDisponibles)
 cont <- cont +1

}

The error, a number with "L":

Image

Print output:

Image

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Dragg
  • 83
  • 2
  • 11
  • 4
    Possible duplicate of: https://stackoverflow.com/questions/13990125/sampling-in-r-from-vector-of-varying-length Read the help page for `sample()`. It behaves differently when the first parameter has length 1. – MrFlick Jun 21 '17 at 17:56

0 Answers0