[R] Take random sample from class variable

Muhammad Subianto msubianto at gmail.com
Sun Aug 6 21:45:15 CEST 2006


Dear all,
Suppose I have a dataset like below, then I take for example, 100
random sample "class" variable where contains "yes" and "no"
respectively, 70% and 30%.
I need a new 100 random sample from mydat dataset, but I can't get the result.
Thanks you very much for any helps.
Best, Muhammad Subianto

mydat <- data.frame(size=c(30,12,15,10,12,12,25,30,20,14),
                       A=c(0,1,0,1,0,1,1,1,0,0),
                       B=c(1,1,0,1,0,1,1,0,0,1),
                       C=c(0,0,1,1,0,0,1,1,0,0),
                       D=c(1,1,1,1,0,1,0,0,1,1),
                       E=c(1,1,0,1,1,1,1,1,1,0),

Class=c("yes","yes","no","yes","yes","no","yes","no","yes","yes"))
mydat
# Maximal data from dataset
max.size <- sum(mydat$size);max.size
# I need sample random
nof.sample <- 100
set.seed(123)
sample.class <- sample(c("yes","no"), nof.sample, prob=c(.7, .3), replace=TRUE)
sample.class
sampledat.class <- mydat[sample.class,]
sampledat.class



More information about the R-help mailing list