# [R] numeric(0)

Gabor Grothendieck ggrothendieck at myway.com
Wed Dec 29 15:29:34 CET 2004

```>
: Date:   Wed, 29 Dec 2004 14:17:35 +0100
: From:   dax42 <Dax42 at web.de>
: To:   <r-help at stat.math.ethz.ch>
: Subject:   [R] numeric(0)
:
:
: Dear all,
:
: I am trying to calculate a score for a string sequence consisting of
: the following four letters: ACGT.
: I have got a matrix giving the scores for each pair of letters.
: So for example the string ACCT has got the pairs: AC, CC and CT.
:
: The matrix has got the following form:
: names<-c("A","C","G","T");
: mscore<-matrix(0,4,4);
: rownames(mscore)<-names;
: colnames(mscore)<-names;
:
: So for the first example pair above I could get the score contained in
: the matrix with the following code:
: >> mscore["A","C"]
:
: 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]];
: }

The above code sutracts 1 from the vector 1:length(sequence) giving
a vector that starts at 0.  I think you meant

1:(length(sequence)-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
:
: What does this mean? Strangely, the command
: "print(score+mscore[sequence[j],sequence[j+1]])"
: works, so it really is the assignment which won't work. Why is that?
: I am running R 2.0 series (GUI version) on Mac OSX 10.3.7.
:
: Any suggestions are welcome.

Try this.  (The first line is to ensure that levels that don't
appear still get counted.)

f <- factor(sequence, levels = names)
sum(table(f[-length(f)], f[-1]) * mscore)

```