Skip to content

for loop or Hmisc library trap.rule function syntax error

4 messages · Neil Skjodt, Frank E Harrell Jr, Brian Ripley +1 more

#
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();

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))
}
...
#
On Fri, 19 Mar 2004 07:49:26 -0700
Neil Skjodt <neil.skjodt at ualberta.ca> wrote:

            
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
---
Frank E Harrell Jr   Professor and Chair           School of Medicine
                     Department of Biostatistics   Vanderbilt University
#
Try counting parentheses: they don't match.  A good editor (e.g. R mode in 
Emacs) would make this instantly obvious.

I doubt if data.df(subj,2:7) is what you want, either: perhaps 
data.df[subj, 2:7].  There is much more in that vain.
On Fri, 19 Mar 2004, Neil Skjodt wrote:

            

  
    
#
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