I am using R-exams and I want to generate a double entry table, but each time with a different set of values (I propose 5 different ones: data1,data2,data3,data4,data5), but it does not work, neither with a counter, nor with "sample".
How could I do it? Thanks
datos1 = c(0 , 2 , 4 , 2, 4 , 8, 13, 6, 12)
datos2 = c(11 , 2 , 4 , 2, 4 , 8, 3, 6, 12)
datos3 = c(12 , 2 , 14 , 2, 4 , 28, 3, 6, 12)
datos4 = c(13 , 2 , 4 , 2, 4 , 8, 3, 6, 12)
datos5 = c(1 , 2 , 4 , 22, 4 , 8, 3, 6, 12)
w9 <- sample(c(datos1, datos2, datos3, datos4, datos5),1)
tabla = cbind (expand.grid (list (Y = c ("3","5","6") ,
X = c ("6","8","9"))), count = w9)
ftable (xtabs(count ∼ Y X, tabla ))
CodePudding user response:
Thanks for the clarification, Amelia :)
c(datos1, datos2, datos3, datos4, datos5)
is combining them in to one long vector, and then sample is just taking 1 single value from these. Instead you want to put them in a list and then sample. This keeps them separate so sample is then looking over the list levels and taking one at random. See below:
w9 <- sample(list(datos1, datos2, datos3, datos4, datos5), 1)
# to extract it from the list back in to the same format as datos1, datos2 etc.
w9 <- unlist(w9)