[R] matrix subset problem with factors

ঋষি ( ऋषि / rIsHi ) r|@h|@d@@roy @end|ng |rom gm@||@com
Wed Feb 20 10:23:53 CET 2019


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
calculation.

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"),
c("A","B","C")))
>
> rNames <- as.factor(c("X","Z"))
> # As some functions from different packages return factors and which
could be overlooked
> rNames
[1] X Z
Levels: X Z
>
> x[rNames,]
  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
>
> sessionInfo()
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

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_3.4.1
>





-- 



With regards
Rishi Das Roy

	[[alternative HTML version deleted]]



More information about the R-help mailing list