jeudi 23 avril 2015

How to edit elements of a 2xn matrix along the rows

I am having some issues working a forloop which allows me to take the following matrix:

> cd
          [,1]      [,2]
[1,] -142.5066 -132.9431
[2,] -161.6038 -166.9276

and renaming the elements along the columns. Specifically, I want do the following:

if cd[1,1] > cd[1,2] , then I want cd[1,2] == 'STOP' else cd[1,2]==cd[1,2]

my code right now for a forloop for K rows is:

for(k in 1:2){ if(cd[1,k]>cd[2,k]){cd[2,k]<-'STOP'} else{cd[2,k]<-cd[2,k]} print(cd) }

The output is the following:

      [,1]               [,2]               
[1,] "-142.50660967154" "-132.943085827163"
[2,] "STOP"             "-166.92760911847" 

     [,1]               [,2]               
[1,] "-142.50660967154" "-132.943085827163"
[2,] "STOP"             "-166.92760911847"

Essentially, after running the loop, I want the result to be:

> cd
          [,1]      [,2]
[1,] -142.5066 -132.9431
[2,]      STOP      STOP

Thank you again.

Aucun commentaire:

Enregistrer un commentaire