Home > Mobile >  How do you test if a matrix exists in a matrix list? (Wordle Project)
How do you test if a matrix exists in a matrix list? (Wordle Project)

Time:09-28

I am an infrequent R users so my apologies if any of my terminology is incorrect. I am working on a project around the game Wordle to see if a given Wordle submission in my family group chat is unique or if they have already been submitted before. The inspiration for this came from the twitter account "Scorigami" which tracks every NFL game and tweets whether or not that score has occurred before in the history of the league.

To load the Wordle entries into R, I've decided to turn each submission into a Matrix where 0 = incorrect letter, 1 = right letter/wrong position, and 2 = right letter/correct position. In R this looks like this:

wordle_brendan <- rbind(c(1,0,0,0,0),c(2,2,0,0,0),c(2,2,0,0,0),c(2,2,2,2,2))
wordle_jack <- rbind(c(2,0,0,0,0),c(2,2,0,0,0),c(2,2,2,2,2))

I then combine them into a list that will be used to check against any future Wordle submissions to see if they have been previously submitted.

list <- list(wordle_brendan, wordle_jack)

I think I am on the right track, but I don't know how to create a new wordle matrix to test whether that submission has been given before. Say I recreated "wordle_brendan" with the same values but under a different name... How would I then get R to check if that matrix exists in my preexisting list of matrices? I've tried using the %in% function 1,000 different ways but can't get it to work.. Any help would be much appreciated! Thanks! (And if you can think of a better way to do this, please let me know!)

CodePudding user response:

There are multiple ways to do this, but this is pretty simple. We need some samples to check:

new1 <- list[[2]]   # The same as your second matrix
new2 <- new1
new2[3, 5] <- 0     # Change one position from 2 to 0.

To compare

any(sapply(list, identical, y=new1))
# [1] TRUE
any(sapply(list, identical, y=new2))
# [1] FALSE

So new1 matches an existing matrix, but new2 does not. To see which matrix:

which(sapply(list, identical, y=new1))
# [1] 2
which(sapply(list, identical, y=new2))
# integer(0)

So new1 matches the second matrix in list, but new2 does not match any matrix.

CodePudding user response:

Here is a way with a matequal function. Base function identical compares objects, not values and if the matrices have the same values but different attributes, such as names, identical returns FALSE.

This is many times too strict. A function that compares values only will return TRUE in these cases.

I will use dcarlson's new1 to illustrate this point.

matequal <- function(x, y) {
  ok <- is.matrix(x) && is.matrix(y) && all(dim(x) == dim(y))
  ok && all(x == y)
}

wordle_brendan <- rbind(c(1,0,0,0,0),c(2,2,0,0,0),c(2,2,0,0,0),c(2,2,2,2,2))
wordle_jack <- rbind(c(2,0,0,0,0),c(2,2,0,0,0),c(2,2,2,2,2))
list <- list(wordle_brendan, wordle_jack)
new1 <- list[[2]]   # The same as your second matrix

wordle_john <- wordle_jack
dimnames(wordle_john) <- list(1:3, letters[1:5])
list2 <- list(wordle_brendan, wordle_jack, wordle_john)

sapply(list2, identical, y=new1)
#> [1] FALSE  TRUE FALSE

sapply(list2, matequal, y=new1)
#> [1] FALSE  TRUE  TRUE

Created on 2022-09-27 with reprex v2.0.2

  • Related