[R] apply

John Sorkin jsorkin at grecc.umaryland.edu
Sat Sep 27 05:46:26 CEST 2008


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)
}

My solution does not work:
 jj<-sapply(c(1,2,3),sample,size=10,replace=TRUE,prob=c(.1,.3,.6))

Suggestions?

John Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
Baltimore VA Medical Center GRECC,
University of Maryland School of Medicine Claude D. Pepper OAIC,
University of Maryland Clinical Nutrition Research Unit, and
Baltimore VA Center Stroke of Excellence

University of Maryland School of Medicine
Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524

(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
jsorkin at grecc.umaryland.edu
Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}



More information about the R-help mailing list