[R] problems understanding sapply

Floris Van Ogtrop f.vanogtrop at usyd.edu.au
Wed Apr 11 03:49:33 CEST 2007


Dear John,

Try the following?

delete<-matrix(nrow=31,ncol=2)
delete[1:31,1]<-1:31

f<- function(X) 
{
	pbinom(X, 30, 0)
}

delete[,2]<-sapply(delete[,1], f)
delete

HTH

Floris

University of Sydney


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of John Sorkin
Sent: Wednesday, 11 April 2007 11:20 AM
To: r-help at stat.math.ethz.ch
Subject: [R] problems understanding sapply

R 2.4.1
windows XP
 
I am trying to fill in a matrix with binomial probabilities without
using a for loop. I am trying to obtain a value for pbinom using the
value stored in column one of the matrix delete. Clearly I am doing
something wrong. Please help me understand my error.
 
> delete<-matrix(nrow=31,ncol=2)
> delete[1:31,1]<-1:31
> delete[,2]<-sapply(delete[,2], pbinom,delete[,1],30,0)
Error in delete[, 2] <- sapply(delete[, 2], pbinom, delete[, 1], 30, 0)
: 
        number of items to replace is not a multiple of replacement
length
 
Thanks,
John
 
 
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 the\ s...{{dropped}}



More information about the R-help mailing list