[R] question about matrix

Thomas W Blackwell tblackw at umich.edu
Wed Nov 12 20:36:22 CET 2003


Mikyoung  -

All answers are "yes", but IMHO you are trying to be
too clever with your data structure.  Programming is
*much* easier if you keep things simple.

Specifically:

(1)  The function  matrix()  will happily build you
a matrix of type "list", with each element of the list
occupying one cell in the matrix.  For example,

tma <- matrix(as.list(letters), nrow=13, ncol=2)
is.list(tma)
[1] TRUE
is.matrix(tma)
[1] TRUE
tma[5,2]
[[1]]
[1] "r"

Same effect with

tma <- as.list(letters)
dim(tma) <- c(13,2)

(2)  Complex numbers - see  help("complex").  This
help page even gives an example which constructs a
matrix of complex numbers.

(3)  To find the rank of matrix  x,  qr(x)$rank.
I don't think there is a specific extractor function
for this component, but I could be wrong.

(4)  But, once again, I think you would be better served by
storing your two values of different types either as two
separate matrices, or as two columns in a data frame, with
additional columns of repetitive integers to indicate the
row and column in your conceptual matrix.

See  help("tapply"),  help("aggregate")  for two functions
which can operate on such a data structure, and  help("gl")
help("col"), help("row")  for functions that will generate
the additional columns.

HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Wed, 12 Nov 2003, Mikyoung Jun wrote:

> Hello,
>
> I have a few questions about matrix in R.
>
> Can we make a matrix whose elements are list? I would like to save two
> different values in each elements of matrix. If there is a package or
> something which can deal with complex numbers, that will do it too. Also,
> I am wondering whether there is a function to calculate the rank of the
> matrix. I found a matrix package, but it doesn't have functions like that.
>
> Thanks a lot in advance!
>
> Mikyoung Jun
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>




More information about the R-help mailing list