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]]
On 05/04/2010 3:20 PM, Trafim Vanishek wrote:> 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!You create the matrix from a list: A <- matrix( as.list(1:10), 10, 1) But now you need to be careful: This won't work A[1,1] <- c(A[1,1],3,4,5) you need A[[1,1]] <- c(A[[1,1]],3,4,5) Duncan Murdoch
Use lists: ?list (but probably assumes you know what a list is already) Relevant sections of "An Introduction to R." (Considerable time and effort have been spent writing this to ease the entry of new users into R. Have you devoted any time or effort to reading it? ) Bert Gunter Genentech Nonclinical Biostatistics -----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 12: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.
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.