[R] apply

Johannes Hüsing johannes at huesing.name
Sat Sep 27 07:20:39 CEST 2008


Am 27.09.2008 um 05:46 schrieb John Sorkin:

> Windows XP
> R 2.7.1
>
> I am trying to use apply (or lapply, sapply) to get the sample 
> function to select 1000 samples of size 10 from c(1,2,3) with 
> probability c(0.1,0.2,0.7), i.e.
>
> for (i in 1:1000)
> {
> j<-sample(c(1,2,3),10,replace=TRUE,prob=c(0.1,0.2,0.7))
> print(j)
> }
>

matrix(sample(c(1, 2, 3), 10000, replace=TRUE, prob=c(.1, .2, .7)), 
nrow=10)

or

replicate(sample(c(1, 2, 3), 10, replace=TRUE, prob=c(.1, .2, .7)), 
1000)



More information about the R-help mailing list