[R] numeric(0)

BXC (Bendix Carstensen) bxc at steno.dk
Wed Dec 29 14:47:29 CET 2004


You would probably be better off without the loop, for example:

ns <- length( sequence )
num.seq <- match( sequence, names )
scores <- mscore[cbind(num.seq[-1],num.seq[-ns])]
sum( scores )

I have used the fact that if you index a matrix with
a two-column,matrix ( here, cbind( , ) ), you select the
corresponding elements of the matrix, see ?"["
But it only works if the matrix is numeric.

Bendix Carstensen
----------------------
Bendix Carstensen
Senior Statistician
Steno Diabetes Center
Niels Steensens Vej 2
DK-2820 Gentofte
Denmark
tel: +45 44 43 87 38
mob: +45 30 75 87 38
fax: +45 44 43 07 06
bxc at steno.dk
www.biostat.ku.dk/~bxc
----------------------

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of dax42
> Sent: Wednesday, December 29, 2004 2:18 PM
> 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]];
> }
> 
> 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.
> Thanks a lot,
> Dax
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://stat.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