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 so...{{dropped}}
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}}
On Tue, 2007-04-10 at 21:19 -0400, John Sorkin wrote:> 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, > JohnJohn, You have a vector (delete[, 1]) being passed to pbinom(). Thus for each value in delete[, 2], pbinom() is returning a vector containing 31 elements:> str(pbinom(delete[, 1], 30, 0))num [1:31] 1 1 1 1 1 1 1 1 1 1 ... By using sapply() in the way you are above, you are essentially getting a double vectorized process with a result of:> str(sapply(delete[,2], pbinom, delete[, 1], 30, 0))num [1:31, 1:31] NA NA NA NA NA NA NA NA NA NA ... That is, a 31 x 31 matrix as a result and you are trying to assign that result to the 31 elements in the second column of delete. All you really need (since pbinom() is already vectorized) is:> pbinom(delete[, 1], 30, 0)[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Presuming that this is end result that you seek. HTH, Marc Schwartz