Hi all I have numerical vector x <- c(1,2,3,3)
and I want to compare all numbers to each other and return a 4 x 4 matrix of TRUE and FALSE indicating if they are identical or not.
Tried to use loops but have not been successful:
matx <- matrix(FALSE,nrow=length(x),ncol=length(x))
for(i in nrow(matx)) {
for (j in ncol(matx)) {
matx[x==x[i],] <- TRUE
}
}
outer
is clearly the way to go. But if you want to use a loop, you need to define your iterators correctly:
x <- c(1,2,3,3)
N <- length(x)
comp = matrix(, nrow=N, ncol=N)
for(i in 1:N) {
for(j in 1:N) {
comp[i, j] = (x[i] == x[j])
}
}
comp
TRUE FALSE FALSE FALSE
FALSE TRUE FALSE FALSE
FALSE FALSE TRUE FALSE
FALSE FALSE FALSE TRUE