1

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.

TarJae
  • 72,363
  • 6
  • 19
  • 66

3 Answers3

2

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)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

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]
deschen
  • 10,012
  • 3
  • 27
  • 50
  • This looks good, but gives a vector of all values that are not 8. I would need just one value that is not 8, – TarJae Jan 30 '22 at 07:36
  • So you want to randomly select one 8 out of all sampled 8‘s? I‘m a bit confused, tbh, what the expected output should be in terms of 8‘s? I.e. you said the uodate should be 4 or 6, and the vector I‘m producing does only contqin 4 and 6, no? – deschen Jan 30 '22 at 08:20
  • Or do you want to sample just ONE row with a 4 and ONE row with a 6? – deschen Jan 30 '22 at 08:23
  • 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. – TarJae Jan 30 '22 at 08:23
  • 1
    With your help I found the answer. `mtcars[sample(which (mtcars[,2] != not_select), 1), 2]`. Please integrate it to your answer and I will accept. – TarJae Jan 30 '22 at 08:29
  • I updated my answer with another potential solution. Would that work as well? – deschen Jan 30 '22 at 08:30
  • 1
    Exactly. but please add the version with which in your answer! Many thanks. – TarJae Jan 30 '22 at 08:32
1

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
benson23
  • 16,369
  • 9
  • 19
  • 38