[R] matrix subset problem with factors
m@rc_@chw@rtz @end|ng |rom me@com
Wed Feb 20 21:59:54 CET 2019
I get the same behavior in R 3.5.2 on macOS.
Others may feel differently, but I am not so sure that this is a bug, as opposed to perhaps the need to clarify in ?Extract, that the following, which is found under Atomic vectors:
"The index object i can be numeric, logical, character or empty. Indexing by factors is allowed and is equivalent to indexing by the numeric codes (see factor) and not by the character values which are printed (for which use [as.character(i)])."
also applies to the indexing of matrices and arrays.
Since matrices and arrays in R are vectors with 'dim' attributes, the behavior is essentially consistent as described above.
Thus, perhaps just add the second sentence above or similar wording to the section for Matrices and arrays.
> On Feb 20, 2019, at 4:23 AM, ঋষি ( ऋषि / rIsHi ) <rishi.dasroy using gmail.com> wrote:
> Hi All,
> I like to report this bug related to matrix subset by rownames when passed
> as factors. Now factors are may not be safe to use but then it should
> generate a warning message. Since many time we use values returned by some
> packages as factor to subset a matrix and which may result in a wrong
> I wish if "factor" is not expected in matrix operation then it should throw
> an error/warning message.
> Below are the codes to reproduce it.
>> x <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"),
>> rNames <- as.factor(c("X","Z"))
>> # As some functions from different packages return factors and which
> could be overlooked
>  X Z
> Levels: X Z
> A B C
> X 1 4 7
> Y 2 5 8
>> ## The intended matrix should return X and Z rows instead of X and Y
> R version 3.4.1 (2017-06-30)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 14.04.5 LTS
> Matrix products: default
> BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
> LAPACK: /usr/lib/lapack/liblapack.so.3.0
>  LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
>  LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
>  LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
>  LC_PAPER=en_GB.UTF-8 LC_NAME=C
>  LC_ADDRESS=C LC_TELEPHONE=C
>  LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
> attached base packages:
>  stats graphics grDevices utils datasets methods base
> loaded via a namespace (and not attached):
>  compiler_3.4.1
> With regards
> Rishi Das Roy
More information about the R-help