I have:
B
nrow=3,
ncol=2)
and I want:
B
[,1] [,2]
[1,] TRUE TRUE
[2,] FALSE TRUE
[3,] FALSE TRUE
as.logical(B)gives a 1D vector. Is there an idiomatic way to do the matrix type conversion?
I'm currently doing the wordy:
boolVector
m
n
m
m
解决方案
mode(B)
This workaround is good for two reasons:
the code is much readable;
the logic works in general (see examples below).
I learnt this trick when reading source code of some packages with compiled code. When passing R data structures to C or FORTRAN functions, some type coercion may be required and they often use mode or storage.mode for this purpose. Both functions preserve attributes of R objects, like "dim" and "dimnames" of matrice