[R] numeric(0)

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Wed Dec 29 14:45:25 CET 2004


On 29-Dec-04 dax42 wrote:
> I am now trying to sum up all the scores with the following code:
> 
> score<-0;
> 
> for(j in 1:length(sequence)-1){
>       score<-score+mscore[sequence[j],sequence[j+1]];
> }
> 
> where sequence is the string sequence.
> Unfortunately, it does not work and I get the following error message:
> 
> Error in "[<-"(`*tmp*`, 1, i, value = numeric(0)) :
>       nothing to replace with

You've fallen into a classic trap: 1:length(sequence)-1 does not
mean what one might naturally expect.

> sequence<-(1:10)
> length(sequence)
[1] 10
> 1:length(sequence)-1
 [1] 0 1 2 3 4 5 6 7 8 9
> 1:(length(sequence)-1)
[1] 1 2 3 4 5 6 7 8 9

In other words, "1:length(sequence)" is constructed first, then
1 is subtracted from every element. As a result, you tried to
read from sequence[0], which isn't there.

However, you can make it evaluate "length(sequence)-1" before
constructing 1:(length(sequence)-1), by using the parantheses
to force precedence.

Cheers,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 29-Dec-04                                       Time: 13:45:25
------------------------------ XFMail ------------------------------




More information about the R-help mailing list