Sample function R does not produce a uniformly distributed sample

I am creating a survey. There are 31 questions; I would like each respondent to answer a subset of 3. I would like them to be managed in random order. Participants must not answer the same questions twice

I created a table with a participant index and a column for question indices for the 1st, 2nd and 3rd questions.

Using the code below, index 31 is underrepresented in my example.

I think I am using the sample function incorrectly. I was hoping someone could help me?

SgPassCode <- data.frame(PassCode=rep(0,10000), QIndex1=rep(0,10000), QIndex2=rep(0,10000), QIndex3=rep(0,10000)) set.seed(123) for (n in 1:10000){ temp <- sample(31,3,FALSE) SgPassCode[n,1] <- n SgPassCode[n,-1] <- temp } d <- c(SgPassCode[,2],SgPassCode[,3],SgPassCode[,4]) hist(d) 
+4
source share
1 answer

The problem is with hist and how she selects her bins, not sample . The proof is the output of table :

 table(d) # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 1003 967 938 958 989 969 988 956 983 990 921 1001 982 1016 1013 959 # 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # 907 918 918 991 931 945 998 1017 1029 980 959 886 947 987 954 

If you want hist work, hist(d, breaks = 0:31) (and, of course, much more) will work.

+8
source

Source: https://habr.com/ru/post/1443952/


All Articles