[R] for loop or Hmisc library trap.rule function syntax error

John Fox jfox at mcmaster.ca
Fri Mar 19 16:39:21 CET 2004


Dear Neil,

In addition to Frank's observations, it's not a good idea to "grow" vectors
(here, control and test) in a loop. Doing so causes the vectors to be copied
over and over. While that doesn't make much difference for vectors with 11
elements, it can make a big difference in a large problem. It's better to
allocate the vector initially [e.g., control <- rep(0, 11)] and then replace
elements in the loop.

I hope this helps,
 John

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Frank 
> E Harrell Jr
> Sent: Friday, March 19, 2004 9:50 AM
> To: Neil Skjodt
> Cc: R-help at stat.math.ethz.ch
> Subject: Re: [R] for loop or Hmisc library trap.rule function 
> syntax error
> 
> On Fri, 19 Mar 2004 07:49:26 -0700
> Neil Skjodt <neil.skjodt at ualberta.ca> wrote:
> 
> > Hello:
> > 
> > I am new R user stumped why the R code after this paragraph 
> generates
> > "Error: syntax error" messages after each of the last 2 
> lines. I have 
> > tried searching the manuals, Hmisc documentation, 
> contributed manuals, 
> > help archives, and Internet. I am running R 1.7.1 under 
> Windows 2000 
> > (I will upgrade when my imminent OS upgrade happens). My data was 
> > successfully entered and displayed as data.df whose first row is 
> > column labels, whose subsequent rows are separate subject results, 
> > whose 2nd to 7th columns are numeric control results (for 
> observations 
> > at 0, 30, 60, 90, 120, and 180 min), and whose 8th to 13th 
> columns are 
> > numeric treatment results. I am trying to enter the control 
> and test 
> > area under the curve values from the Hmisc trap.rule function into 
> > "control" and "test" for hypothesis testing. I tried 
> posting this last 
> > PM, but my message seems to have been lost. I apologize if this 
> > appears as a duplicate message. Thanks. Neil
> > 
> > ...
> > control = c();		 
> > test = c();
> 
> You need to spend time with the manuals.  Remove ; from end 
> of line, set aside vectors of full length, use [ ] for 
> subscripting and subsetting; remove rownum; recognize that 
> first row should not be "column labels".
> 
> Frank Harrell
> 
> > 
> > for (subj in 1:11) {
> > 
> >   rownum = subj+1	  # add one to subject as first row is 
> column labels
> >   control(subj)=trap.rule(c(0,30,60,90,120,180),c(data.df(subj,2:7))
> >   test(subj)=trap.rule(c(0,30,60,90,120,180),c(data.df(subj,8:13))
> > }
> 
> ---
> Frank E Harrell Jr   Professor and Chair           School of Medicine
>                      Department of Biostatistics   Vanderbilt 
> University
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html




More information about the R-help mailing list