[R] How generate random numbers from given vector???

Muhuri, Pradip (SAMHSA/CBHSQ) Pradip.Muhuri at samhsa.hhs.gov
Fri Oct 26 03:05:53 CEST 2012


Hello,

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

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


Pradip Muhuri

________________________________________
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Rui Barradas [ruipbarradas at sapo.pt]
Sent: Thursday, October 25, 2012 7:19 PM
To: Rlotus
Cc: r-help at r-project.org
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.
>
> ______________________________________________
> 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.

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




More information about the R-help mailing list