I understand that I'm assigning a sample with binary values with replacement and probability of the value being 0 of 90% and 1 of 10%. What I don't understand is the first part:
test[disease==0] <- sample(c(0,1)...
Usually square brackets would be to indicate the index e.g., 'test[1]', but in this context, I just don't understand what is happening with a conditional in square brackets. Can anyone assist?
Please, see more of the context in the following code:
disease and test are separate vectors neither inside the other however they are the same length, so the author presumably intents to utilise a natural alignment. The key to understanding what is happening in your example is to think about the general computer science concept of indexing into (an array). square brackets in R are used to index into a vector
Here is some illustrative code that I hope will help you
# setup
(sqrs <- (1:3)^2)
(l <- letters[1:3])
#direct use
sqrs[1]
l[1]
#indirect use
number <- 1
sqrs[number]
l[number]
# again with logicals (what to include what to not) rather than the numbers of what to include
(tf <- c(TRUE,FALSE,TRUE))
sqrs[tf]
l[tf]