Hi, kindly I want to select randomly and item from list of items. the list generated in a looping process. I used sample(mylist,1) it works fine. BUTsome times the list have only one item. that should be chosen in this case since there is no other one. I found that sample return different item not included in the list thanks in advance RAE [[alternative HTML version deleted]]
Hi, I am not sure I understand the problem.? Please provide a reproducible example using ?dput(). mylist <- list(1:3, LETTERS[1:2], rnorm(4)) sample(mylist,1) sample(mylist,1) ?mylist1 <- list(1:2) sample(mylist1,1) #[[1]] #[1] 1 2 ?sample(mylist1,1) #[[1]] #[1] 1 2 A.K. On Thursday, May 22, 2014 3:55 AM, Ragia Ibrahim <ragia11 at hotmail.com> wrote: Hi, kindly I want to select randomly and item from list of items. the list generated in a looping process. I used sample(mylist,1) it works fine. BUTsome times the list have only one item. that should be chosen in this case since there is no other one. I found that sample return different item not included in the list thanks in advance RAE ??? ??? ??? ? ??? ??? ? ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
On Thu, 22 May 2014 09:54:13 AM Ragia Ibrahim wrote:> Hi, > kindly I want to select randomly and item from list of items. the list > generated in a looping process. I used sample(mylist,1) it works fine. > BUTsome times the list have only one item. that should be chosen inthis> case since there is no other one. I found that sample return differentitem> not included in the list thanks in advance > RAE >Hi RAE, This doesn't happen in an example like this: for(i in 1:5) { testlist<-list() for(j in 1:i) testlist[[j]]<-sample(LETTERS[1:26],1) cat("list is\n") print(testlist) cat("sample is\n") print(sample(testlist,1)) } How are you generating your lists? Jim