Masi Masi - 19 days ago 8
R Question

How to create binary matrix from numerical matrix in R?

Pseudocode for matrix operation that I try to find out


  1. If matrix cell value < alpha, put 1.

  2. Else 0.






I want to create a binary matrix with the alpha from p-value matrix
p.mat
but I cannot handle
apply
correctly where I try satisfy the pseudocode.

First approach

# http://stackoverflow.com/a/4236383/54964
new <- apply(p.mat.p, 1, function(x)
if (alpha > x) {
x <- 0
} else {
x <- 1
}
)


Second approach but fails

new <- apply(p.mat.p, 1, function(x)
x <- (x < alpha)
)
print(new)
#Error in match.fun(FUN) : argument "FUN" is missing, with no default
#Calls: apply -> match.fun
#Execution halted


Trial and Code

library("psych")

ids <- seq(1,11)
M.cor <- cor(mtcars)
colnames(M.cor) <- ids
rownames(M.cor) <- ids

p.mat <- psych::corr.test(M.cor, adjust = "none", ci = F)

p.mat.p <- p.mat[["p"]]

alpha <- .00000005

# http://stackoverflow.com/a/4236383/54964
new <- apply(p.mat.p, 1, function(x)
if (alpha > x) {
x <- 0
} else {
x <- 1
}
)

#Error in alpha > x :
# comparison (6) is possible only for atomic and list types
#Calls: sapply -> lapply -> FUN
#Execution halted


Example with a square matrix and a p-value for alpha.

Input: nxn matrix with a p-value =
p.mat.p


# str(p.mat.p)
num [1:11, 1:11] 0.00 4.04e-09 1.09e-09 4.32e-06 1.78e-05 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:11] "1" "2" "3" "4" ...
..$ : chr [1:11] "1" "2" "3" "4" ...


# 1 2 3 4 5
#1 0.000000e+00 4.037623e-09 1.091445e-09 4.322152e-06 1.780708e-05
#2 4.037623e-09 0.000000e+00 1.659424e-09 5.625666e-07 5.174268e-05
#3 1.091445e-09 1.659424e-09 0.000000e+00 1.304240e-05 4.935086e-06
...


Expected output: nxn binary matrix of ones and zeros, alpha=0.2 and the intended output is

[,1] [,2] [,3] [,4] [,5]
[1,] FALSE TRUE FALSE TRUE FALSE
[2,] TRUE TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE TRUE


R: 3.3.1

OS: Debian 8.5

hhh hhh
Answer

Do

alpha <- .00000005
p.mat.p <- (p.mat.p < alpha)
str(p.mat.p)
print(p.mat.p)

Output

 logi [1:11, 1:11] TRUE TRUE TRUE FALSE FALSE TRUE ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:11] "1" "2" "3" "4" ...
  ..$ : chr [1:11] "1" "2" "3" "4" ...
       1     2     3     4     5     6     7     8     9    10    11
1   TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
2   TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
3   TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
4  FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
5  FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
6   TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
7  FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
8  FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
9  FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
10 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
11 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
Comments