Reputation: 79112
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.
Upvotes: 1
Views: 990
Reputation: 389105
Perhaps, another way -
tmp <- mtcars[, 2]
sample(tmp[tmp != not_select], 1)
The above gives the probability of selecting each value based on their occurrence in the dataset. If you want the probability to be equal irrespective of how many times they occur you may only consider unique
values.
tmp <- unique(mtcars[, 2])
sample(tmp[tmp != not_select], 1)
Upvotes: 2
Reputation: 19107
This recursive function will run on itself again if the output matches not_selected
.
exclude_not_selected <- function(not_selected) {
value <- mtcars[sample(1:nrow(mtcars), 1),2]
if (value == not_selected) {
exclude_not_selected(not_selected)
} else {
return(value)
}
}
exclude_not_selected(8)
[1] 4
Upvotes: 1
Reputation: 11006
Couldn‘t you just add a filtering condition based on not_select?
mtcars[sample(1:nrow(mtcars), 1) & mtcars$cyl != not_select, 2]
Update: how about:
not_select <- 8
draw_cyl <- sample(unique(mtcars$cyl[mtcars$cyl != not_select]), 1)
mtcars %>%
filter(cyl == draw_cyl) %>%
slice_sample(n = 1) %>%
pull(cyl)
Or as suggested by TarJae themselve (so I don‘t own any credit for it!):
mtcars[sample(which (mtcars[,2] != not_select), 1), 2]
Upvotes: 1