I'm struggling to apply a function to each element of a matrix, a lower triangular Jaccard similarity matrix.
The function should return values of the matrix with values > .7, and reassign other elements as NA, making it easier to identify highly similar binary variables. Ideally the matrix structure is preserved.
I've created a simple sample 3x3 matrix populated with random values for testing:
N <- 3 # Observations
N_vec <- 3 # Number of vectors
set.seed(123)
x1 <- runif(N * N_vec)
mat_x1 <- matrix(x1, ncol = N_vec)
mat_x1[upper.tri(mat_x1)] <- NA
diag(mat_x1) <- NA
mat_x1
[,1] [,2] [,3]
[1,] NA NA NA
[2,] 0.7883051 NA NA
[3,] 0.4089769 0.0455565 NA
How do I apply the following function to each matrix element that returns values > 0.7?
y = (function(x) if (x > .7) { return(x) } else { return(NA) })
I'd like to see the following after applying the function:
mat_x2
[,1] [,2] [,3]
[1,] NA NA NA
[2,] 0.7883051 NA NA
[3,] NA NA NA
In this case, you may just do :
mat_x1[mat_x1 <= .7] <- NA
# [,1] [,2] [,3]
#[1,] NA NA NA
#[2,] 0.7883051 NA NA
#[3,] NA NA NA
In case, this is just an example and you want to apply some kind of variation of function y
you may do the following. First make sure that your function is vectorized and can handle multiple values which in this case is as simple as changing if
to ifelse
and then apply the function to the matrix.
y = function(x) ifelse(x > .7, x, NA)
y(mat_x1)