Hi,
With the sample() method and some conditional logic you should be able to do this:
library(dplyr)
set.seed(20) #Just here for reproducibility
nParticipants = 8
mySurvey = data.frame(
Q1 = sample(c("Student", "Engineer", "Doctor"), nParticipants,
replace = T, prob = c(0.6,0.2,0.2))
)
mySurvey = mySurvey %>% mutate(
Q2 = ifelse(Q1 == "Student", sample(1:10, nParticipants, replace = T), NA)
)
mySurvey
#> Q1 Q2
#> 1 Engineer NA
#> 2 Doctor NA
#> 3 Student 9
#> 4 Student 8
#> 5 Engineer NA
#> 6 Engineer NA
#> 7 Student 9
#> 8 Student 5
Created on 2021-07-03 by the reprex package (v2.0.0)
When using continuous distributions (sample() is for discrete), you have a lot of known types to sample from all built into R
set.seed(20)
nParticipants = 8
#Uniform distribution
runif(nParticipants, 100000, 250000)
#> [1] 231628.2 215280.0 141844.5 179374.6 244436.1 247053.2 113699.9 110612.4
#Normal distribution
rnorm(nParticipants, 5, 1)
#> [1] 4.553433 5.569606 2.110282 4.130982 4.538297 4.444459 4.979865 4.849618
#Others
# runif, rpois, rmvnorm, rnbinom, rbinom, rbeta, rchisq, rexp, rgamma,
# rlogis, rstab, rt, rgeom, rhyper, rwilcox, rweibull
Hope this helps,
PJ