How generate random numbers from given vector???

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How generate random numbers from given vector???

Rlotus
I wanna generate random numbers from a vector...

for example number<-c(0,1,3,4,5,6,8)
 so
 
rsidp<-function(x){
        i=0
        for (i in seq(1:x))

        {y<-sample(number,x, replace=T)}
        return(y)
}
 so all random numbers have to be from vector "number";
so if I type rsidp(5)..... it has to give me 5 random numbers except 2,7,9 (because they are not in the vector "numbers"). help me plz with it (((



Reply | Threaded
Open this post in threaded view
|

Re: How generate random numbers from given vector???

arun kirshna
This post has NOT been accepted by the mailing list yet.
Hi,
Try this:
number1<-c(0,1,3,4,5,6,8)
 rsidp<-function(x){
 y<-sample(x,5,replace=TRUE)
 y
 }
 rsidp(number1)
#[1] 3 0 6 8 4
 rsidp(number1)
#[1] 1 8 8 6 4
 rsidp(number1)
#[1] 8 3 6 6 6
A.K.
Reply | Threaded
Open this post in threaded view
|

Re: How generate random numbers from given vector???

arun kirshna
In reply to this post by Rlotus

Hi,
Try this:
number1<-c(0,1,3,4,5,6,8)
 rsidp<-function(x){
 y<-sample(x,5,replace=TRUE)
 y
 }
 rsidp(number1)
#[1] 3 0 6 8 4
 rsidp(number1)
#[1] 1 8 8 6 4
 rsidp(number1)
#[1] 8 3 6 6 6
A.K.


----- Original Message -----
From: Rlotus <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, October 25, 2012 3:24 PM
Subject: [R] How generate random numbers from given vector???

I wanna generate random numbers from a vector...

for example number<-c(0,1,3,4,5,6,8)
so

rsidp<-function(x){
    i=0
    for (i in seq(1:x))

    {y<-sample(number,x, replace=T)}
    return(y)
}
so all random numbers have to be from vector "number";
so if I type rsidp(5)..... it has to give me 5 random numbers except 2,7,9
(because they are not in the vector "numbers"). help me plz with it (((







--
View this message in context: http://r.789695.n4.nabble.com/How-generate-random-numbers-from-given-vector-tp4647447.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[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: How generate random numbers from given vector???

Rlotus
In reply to this post by Rlotus
thank u so much! i got it.
Reply | Threaded
Open this post in threaded view
|

Re: How generate random numbers from given vector???

Rui Barradas
In reply to this post by Rlotus
Hello,

You don't need the loop, the sample() argument 'size' is there for that.
See 'sample.

number <- c(0,1,3,4,5,6,8)
rsidp <- function(n) sample(number, n, replace = TRUE)
rsidp(5)

Hope this helps,

Rui Barradas
Em 25-10-2012 20:24, Rlotus escreveu:

> I wanna generate random numbers from a vector...
>
> for example number<-c(0,1,3,4,5,6,8)
>   so
>  
> rsidp<-function(x){
> i=0
> for (i in seq(1:x))
>
> {y<-sample(number,x, replace=T)}
> return(y)
> }
>   so all random numbers have to be from vector "number";
> so if I type rsidp(5)..... it has to give me 5 random numbers except 2,7,9
> (because they are not in the vector "numbers"). help me plz with it (((
>
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-generate-random-numbers-from-given-vector-tp4647447.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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: How generate random numbers from given vector???

Muhuri, Pradip (SAMHSA/CBHSQ)

Hello,

The other options is to use the sample() function.

test2 <- matrix (rep(sample(number1, size = 5), times=3), nrow=3)


Pradip Muhuri

________________________________________
From: [hidden email] [[hidden email]] On Behalf Of Rui Barradas [[hidden email]]
Sent: Thursday, October 25, 2012 7:19 PM
To: Rlotus
Cc: [hidden email]
Subject: Re: [R] How generate random numbers from given vector???

Hello,

You don't need the loop, the sample() argument 'size' is there for that.
See 'sample.

number <- c(0,1,3,4,5,6,8)
rsidp <- function(n) sample(number, n, replace = TRUE)
rsidp(5)

Hope this helps,

Rui Barradas
Em 25-10-2012 20:24, Rlotus escreveu:

> I wanna generate random numbers from a vector...
>
> for example number<-c(0,1,3,4,5,6,8)
>   so
>
> rsidp<-function(x){
>       i=0
>       for (i in seq(1:x))
>
>       {y<-sample(number,x, replace=T)}
>       return(y)
> }
>   so all random numbers have to be from vector "number";
> so if I type rsidp(5)..... it has to give me 5 random numbers except 2,7,9
> (because they are not in the vector "numbers"). help me plz with it (((
>
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-generate-random-numbers-from-given-vector-tp4647447.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

______________________________________________
[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: How generate random numbers from given vector???

Jorge I Velez
You can also just replicate() as follows:

# x is the vector
# s is the size of the sample
# B is the number of samples
# ... arguments passed to sample()
f <- function(x, s, B, ...) replicate(B, sample(x, s, ...))
f(x, 3, 10, TRUE)
f(x, 3, 10, FALSE)

 HTH,
Jorge.-


On Fri, Oct 26, 2012 at 12:05 PM, Muhuri, Pradip (SAMHSA/CBHSQ) <
[hidden email]> wrote:

>
> Hello,
>
> The other options is to use the sample() function.
>
> test2 <- matrix (rep(sample(number1, size = 5), times=3), nrow=3)
>
>
> Pradip Muhuri
>
> ________________________________________
> From: [hidden email] [[hidden email]] On
> Behalf Of Rui Barradas [[hidden email]]
> Sent: Thursday, October 25, 2012 7:19 PM
> To: Rlotus
> Cc: [hidden email]
> Subject: Re: [R] How generate random numbers from given vector???
>
> Hello,
>
> You don't need the loop, the sample() argument 'size' is there for that.
> See 'sample.
>
> number <- c(0,1,3,4,5,6,8)
> rsidp <- function(n) sample(number, n, replace = TRUE)
> rsidp(5)
>
> Hope this helps,
>
> Rui Barradas
> Em 25-10-2012 20:24, Rlotus escreveu:
> > I wanna generate random numbers from a vector...
> >
> > for example number<-c(0,1,3,4,5,6,8)
> >   so
> >
> > rsidp<-function(x){
> >       i=0
> >       for (i in seq(1:x))
> >
> >       {y<-sample(number,x, replace=T)}
> >       return(y)
> > }
> >   so all random numbers have to be from vector "number";
> > so if I type rsidp(5)..... it has to give me 5 random numbers except
> 2,7,9
> > (because they are not in the vector "numbers"). help me plz with it (((
> >
> >
> >
> >
> >
> >
> >
> > --
> > View this message in context:
> http://r.789695.n4.nabble.com/How-generate-random-numbers-from-given-vector-tp4647447.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [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.
>
> ______________________________________________
> [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.
>

        [[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.