[R] numeric(0)

Sean Davis sdavis2 at mail.nih.gov
Wed Dec 29 14:50:30 CET 2004


On Dec 29, 2004, at 8:17 AM, dax42 wrote:

> 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]];
> }
>

Is sequence a string?  If so, you will probably need to make some 
modifications like these:

 > sequence <- 'ACGT'
 > sequence[1]
[1] "ACGT"
 > substr(sequence,1,1)
[1] "A"
 > substr(sequence,2,1)
[1] ""
 > substr(sequence,1,2)
[1] "AC"
 > substr(sequence,2,2)
[1] "C"
 > substr(sequence,3,3)
[1] "G"
 > mscore <- matrix(runif(16),4,4)
 > names <- c('A','C','G','T')
 > rownames(mscore) <- names
 > colnames(mscore) <- names
 > mscore
           A         C         G          T
A 0.6200289 0.6324337 0.1895207 0.28253473
C 0.5026072 0.6552428 0.7978809 0.43131540
G 0.1669823 0.8808445 0.6021024 0.01563101
T 0.4184646 0.9620714 0.7723088 0.33045464
 > mscore[substr(sequence,1,1),substr(sequence,2,2)]
[1] 0.6324337
 > score <- 0
 > for (j in 1:(nchar(sequence)-1)) {score <- score+ 
mscore[substr(sequence,j,j),substr(sequence,j+1,j+1)]}
 > score
[1] 1.445946

Hope this helps.  I imagine this isn't the most efficient way of 
solving this problem, though.

Sean




More information about the R-help mailing list