dimanche 30 janvier 2022

How to exclude values from random selection

This is independent but related to this question Randomly take wrong answers of a quiz question from dataframe column, instead of doing by hand

Using the mtcars dataset I now have managed to randomly select one value from a certain column: In this example from cyl column.

mtcars[sample(1:nrow(mtcars), 1),2]

This code will give randomly [1] 6 or [1] 8 or ...

Now I want to exclude one certain value to be chosen, in this example say cyl==8. I would store the value in a vector like:

not_select <- 8

mtcars[sample(1:nrow(mtcars), 1),2]

My question: How can I integrate not_select in mtcars[sample(1:nrow(mtcars), 1),2]

Expected Output: The random sample should not include 8

UPDATE: e.g. the output should be: 6 or 4

UPDATE II due to unclear situation:

I want to select from column cyl one value randomly. This value should not be for example 8. So the value will be 4 or 6.

Explanation: 8 is the correct answer. And I am constructing randomly false answers with the other values (e.g. 4 and 6) from cyl column.




Aucun commentaire:

Enregistrer un commentaire