vendredi 29 juin 2018

Exhaustive Search Through Inverse Coordinates

I have a table of TRUE and FALSE values and would like to create a list/table based on the pattern of those TRUE and FALSE values.

Specifically, I want to consider the relation of coordinates with inverted coordinates.

For example, if the cell mydata[x,y] has a value of TRUE, and the cell mydata[y,x] has a value of TRUE, I want to record the column name of mydata[x,y] and the column name of mydata[y,x] with a "<->" printed in between.

I am able to cycle through the number of rows using the following code, but this does not consider the pattern of TRUE and FALSE exhaustively. It is restricted by the number of rows. Without this restriction, the for loop ends up out of bounds.

What can I do with my code to consider all of the TRUE and FALSE combinations of coordinates/inverted coordinates without going out of bounds?

random.base <- data.frame(replicate(30,sample(0:1,30,rep=TRUE)))
boolean.table <- random.base == "1"

# this code varies the distance of i and j, but it only records 29 patterns 

output <- NULL

for(i in 1:(nrow(boolean.table)-1)){
  print(i)
  for(j in (i+1):nrow(boolean.table)){
    if(boolean.table[i,j] == "TRUE" & boolean.table[j,i] == "TRUE"){
      output[i] <- paste(colnames(boolean.table)[i], "<->", colnames(boolean.table)[j])
} else if(boolean.table[i,j] == "TRUE" & boolean.table[j,i] == "FALSE"){
  output[i] <- paste(colnames(boolean.table)[i], "<-", colnames(boolean.table)[j])
} else if(boolean.table[i,j] == "FALSE" & boolean.table[j,i] == "TRUE"){        
  output[i] <- paste(colnames(boolean.table)[i], "->", colnames(boolean.table)[j])
} else {
  NULL
}
}
}

Aucun commentaire:

Enregistrer un commentaire