[R] Matrix elements are vectors

Daniel Malter daniel at umd.edu
Mon Apr 5 21:38:37 CEST 2010


I may be mistaken, but I don't think that's possible or even should be
possible. A matrix is m x n, where m and n are (kind of fixed) integers. You
cannot have a matrix where m(1) to m(n) (the row lengths) vary. If you want
to do this, you have to use a list instead (I believe).

As a poor workaround you could fill an m x n matrix with NAs or 0s (where n
is the anticipated maximum length among all rows) and then fill the rows
with the values. However, this is dirty as you will not be able to
distinguish true NAs or 0s at the end of a row from the ones you have used
to fill the dummy matrix in the first place; so the list is really the way
to go.

x1=c(1,2,3)
x2=c(4,5,6)

y1=c(0,1,2,3)
y2=c(4,5,6,7)

myList=list(x1,y1)
myList

myList[[1]]=append(myList[[1]],x2)
myList[[2]]=append(myList[[2]],y2)
myList

HTH,
Daniel



-------------------------
cuncta stricte discussurus
-------------------------

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Trafim Vanishek
Sent: Monday, April 05, 2010 3:21 PM
To: r-help at r-project.org
Subject: [R] Matrix elements are vectors

Dear all,

My question how is it possible to define a matrix A with 10 rows 1 column,
so that its elements are vectors of undefined length.

I need to have a possibility later to add elements like A[1,1] <-
c(A[1,1],3,4,5)

Thanks a lot for the help!

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list