select randomly from a list

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

select randomly from a list

Ragia
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: select randomly from a list

arun kirshna


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 <[hidden email]> 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]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: select randomly from a list

Jim Lemon
In reply to this post by Ragia
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 is no other one. I found that sample return different
item
> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: select randomly from a list

Peter Dalgaard-2
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, replace = FALSE, prob = NULL)
       x[sample.int(length(x), size, replace, prob)]
> replicate(10,Sample(5:5, 1))
 [1] 5 5 5 5 5 5 5 5 5 5

(It's one of the cases of misguided user-friendliness that is probably long regretted by its authors, but has been around for so long that it is painful to change because code relies on the current behavior. A similar case is diag().)

-pd

On 22 May 2014, at 11:05 , Jim Lemon <[hidden email]> wrote:

> 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 is no other one. I found that sample return different
> item
>> 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
>
> ______________________________________________
> [hidden email] 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.

--
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: select randomly from a list

Boris Steipe
In reply to this post by Jim Lemon
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. this is the same as
sample(1:5,10, replace=TRUE)

#An easy way to catch this is:

if (length(a) > 1) sample(a,1) else a



Boris



On 2014-05-22, at 5:05 AM, Jim Lemon wrote:

> 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 is no other one. I found that sample return different
> item
>> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.