Hi everyone, I have a dataset(named "Mydata") which includes 4 different variables named; s1,s2,s3,s4 .Each variable(symptom) has 14 patients. I need to use random sampling to make, 5 different samples from my data with 5 patients in each sample. i.e. using all 4 variables I need to make 5 different samples by changing patients(with 5 patients in each sample). X8 X9 X10 X102 X110 X177 X283 X284 X286 X292 X297 X306 X308 X314 s1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 s2 0 0 0 0 1 0 0 0 0 0 0 0 1 0 s3 0 1 0 0 0 0 0 0 0 0 0 0 1 0 s4 1 0 0 0 1 0 0 0 0 1 0 0 0 0 I used this code: temp=list(NULL) for(i in 1:5) {temp[i]<-sample(Mydata,5, replace=F)} show(temp) but I get the following error: "number of items to replace is not a multiple of replacement length" any idea why I get this eeror message and how can I fix it? Thanks a lot. -- View this message in context: http://www.nabble.com/Help-Help-with-sampling-tp19994275p19994275.html Sent from the R help mailing list archive at Nabble.com.
Alex99 wrote:> Hi everyone, > > I have a dataset(named "Mydata") which includes 4 different variables named; > s1,s2,s3,s4 .Each variable(symptom) has 14 patients. > I need to use random sampling to make, 5 different samples from my data with > 5 patients in each sample. i.e. using all 4 variables I need to make 5 > different samples by changing patients(with 5 patients in each sample). > > > X8 X9 X10 X102 X110 X177 X283 X284 X286 X292 X297 X306 X308 X314 > s1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 > s2 0 0 0 0 1 0 0 0 0 0 0 0 1 0 > s3 0 1 0 0 0 0 0 0 0 0 0 0 1 0 > s4 1 0 0 0 1 0 0 0 0 1 0 0 0 0 > > > I used this code: > > temp=list(NULL) > for(i in 1:5) {temp[i]<-sample(Mydata,5, replace=F)} show(temp) > > but I get the following error: > "number of items to replace is not a multiple of replacement length" > > any idea why I get this eeror message and how can I fix it? > Thanks a lot. > >The direct cause is that you are not using temp[[i]]<-, but I don't think that sample() construct does what I think you think it does either. You might want to use replicate() instead, as in replicate(2,airquality[sample(1:153,5, replace=F),], simplify=F) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Dear Alex, Is this what you want? # Data set my=read.table(textConnection(" X8 X9 X10 X102 X110 X177 X283 X284 X286 X292 X297 X306 X308 X314 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0"),header=TRUE) closeAllConnections() rownames(my)=paste('s',1:4,sep="") # Samples res=list() for(i in 1:5) res[[i]]<- my[,sample(colnames(my),5)] res HTH, Jorge On Wed, Oct 15, 2008 at 10:07 AM, Alex99 <loyola9988@yahoo.com> wrote:> > Hi everyone, > > I have a dataset(named "Mydata") which includes 4 different variables > named; > s1,s2,s3,s4 .Each variable(symptom) has 14 patients. > I need to use random sampling to make, 5 different samples from my data > with > 5 patients in each sample. i.e. using all 4 variables I need to make 5 > different samples by changing patients(with 5 patients in each sample). > > > X8 X9 X10 X102 X110 X177 X283 X284 X286 X292 X297 X306 X308 X314 > s1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 > s2 0 0 0 0 1 0 0 0 0 0 0 0 1 0 > s3 0 1 0 0 0 0 0 0 0 0 0 0 1 0 > s4 1 0 0 0 1 0 0 0 0 1 0 0 0 0 > > > I used this code: > > temp=list(NULL) > for(i in 1:5) {temp[i]<-sample(Mydata,5, replace=F)} show(temp) > > but I get the following error: > "number of items to replace is not a multiple of replacement length" > > any idea why I get this eeror message and how can I fix it? > Thanks a lot. > > -- > View this message in context: > http://www.nabble.com/Help-Help-with-sampling-tp19994275p19994275.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]