-9

Here is my code:

Sip<-function(S,u,v){
   S<-matrix(,u,v);u<-nrow(S);v<-ncol(S);u==v;
   for(i in 1:u)
     {for(j in 1:v){
       if(S[i,j]==S[j,i]){print("Simétrica")}
       else{print("No simétrica")}
     }
   }
   S
 }
 Sip(S,2,2)

My error is here: if (S[i, j] == S[j, i]) {:

Error:

missing value where TRUE/FALSE needed

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

4

You seem to be a little confused. You are passing a matrix S (which you haven't defined in the code you have posted here) to your function. That doesn't cause an error because inside your function you define a u by v matrix filled with NA values (try matrix(,2,2) to see), which overrides the value of S passed in and triggers the error.

Try this:

Sip <- function(S,u,v){
   for(i in 1:u)
     {for(j in 1:v){
       if(S[i,j]==S[j,i]){print("Simétrica")}
       else{print("No simétrica")}
     }
   }
   S
 }
 S <- matrix(1:4,2,2)
 Sip(S,2,2)
## [1] "Simétrica"
## [1] "No simétrica"
## [1] "No simétrica"
## [1] "Simétrica"
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

That still might not be what you want, but it gets one step further ...

Also possibly useful:

apropos("symmetric")
isSymmetric(S)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453