Re: [R] select randomly from a list

2014-05-22 Thread Boris Steipe
You are probably encountering an annoying behaviour of sample(): when it is given exactly one integer as an argument, it takes this as the upper limit of a range. a <- c(3,5) sample(a,10, replace=TRUE) #[1] 5 5 3 3 3 3 3 3 3 5 a <- c(5) sample(a,10, replace=TRUE) #[1] 2 1 3 1 1 3 4 5 1 4 #i.e

Re: [R] select randomly from a list

2014-05-22 Thread peter dalgaard
It's a well known quirk of sample that it changes behavior when the x argument has length 1: > replicate(10,sample(4:5, 1)) [1] 5 4 5 4 5 4 5 4 4 4 > replicate(10,sample(5:5, 1)) [1] 5 3 1 1 1 2 5 3 2 2 One workaround is to zap the offending branch inside sample: > Sample <- function (x, size

Re: [R] select randomly from a list

2014-05-22 Thread Jim Lemon
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 in this > case since there

Re: [R] select randomly from a list

2014-05-22 Thread arun
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

[R] select randomly from a list

2014-05-22 Thread Ragia Ibrahim
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 n