[S] Re : Question - Coding Numeric Vectors

Scherer, Peter (pscherer@dowagro.com)
Wed, 25 Mar 1998 11:01:10 -0500


I would like to thank Coen Bernaards, Alan Zaslavsky, Tony Plate, Robert
Balshaw, Lutz Prechelt, Todd Taylor and Christian Keller for their
suggestions on coding a numeric vector using breaks or groups defined in
another vector.

All suggestions dealt with either sample or cut (which I had previously
tried).

Christian Keller's suggestion using sample was to use sample to generate
a multinomial sample by :

codes <- 1:5 # assume the feeding sites are coded as 1,2,3,4,5
probs <- c(.25, .35, .4, 0, 0) # the probabilities for codes
sample(codes, replace=T, size=20, prob=probs)

Tony Plate's suggestion was to use cut in the following manner :

> probs <- c(.25, .35, .4, 0, 0)
> sites <- cut(runif(20),c(0,cumsum(probs)),inc=T)
> sites
[1] 2 1 3 1 1 2 3 3 3 2 1 3 2 2 3 2 1 2 3 2

Each of the approaches presented provided a solution to my problem.
Thanks to all who responded

Peter Scherer
Dow AgroSciences
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news