[R] runif with weights
Bert Gunter
gunter.berton at gene.com
Thu May 31 00:25:33 CEST 2007
You did not explicitly say it, but your example indicates that you want to
sample from integers only (else what would weights mean?). So...
?sample -- in particular note the prob argument and read help docs
carefully
e.g.
sample(100,25,prob=c(0,rep.int(.4,9),rep.int(.6,90))) ## without replacement
Bert Gunter
Genentech Nonclinical Statistics
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ken Knoblauch
Sent: Wednesday, May 30, 2007 5:59 PM
To: Joao.Fadista at agrsci.dk
Cc: r-help at stat.math.ethz.ch
Subject: [R] runif with weights
Not sure why you have set the probability of a 1 to 0 but maybe something
like this might be what you want:
round( ifelse( rbinom(25, 1, 0.4), runif(25, 2, 10), runif(25, 11, 100) ) )
[1] 2 6 34 90 79 71 83 8 47 36 21 32 17 71 3 16 9 65 94 6 30 5 7
10 13
############
I would like to generate 25 numbers from 1 to 100 but I would like to have
some numbers that could be more probable to come out. I was thinking of
the function runif:
runif(25, 1, 100) , but I don´t know how to give more weight to some
numbers.
Example:
each number from 2 to 10 has the probability of 40% to come out but the
probability of each number from 11 to 100 to come out is 60%.
--
Ken Knoblauch
Inserm U846
Institut Cellule Souche et Cerveau
Département Neurosciences Intégratives
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.pizzerialesgemeaux.com/u846/
______________________________________________
R-help at stat.math.ethz.ch 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