

Hi,
I have several matrix in a list, for example:
e
[[1]]
[,1] [,2]
[1,] 1 3
[2,] 2 4
[[2]]
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
[[3]]
[,1] [,2]
[1,] 2 1
I would like to join them by column i.e.
[,1] [,2] [,3] [,4][,5] [,6]
[1,] 1 3 1 4 2 1
[2,] 2 4 2 5 NA NA
[3,] NA NA 3 6 NA NA
I have tried do.call(cbind,e) but I get this error message as the rows are of different length
Error in function (..., deparse.level = 1) :
number of rows of matrices must match (see arg 2)
Can anyone advise me please?
Thanks Emma


try this:
matLis < list(matrix(1:4, 2, 2), matrix(1:6, 3, 2),
matrix(2:1, 1, 2))
n < max(sapply(matLis, nrow))
do.call(cbind, lapply(matLis, function (x)
rbind(x, matrix(, nnrow(x), ncol(x)))))
I hope it helps.
Best,
Dimitris
On 1/6/2011 11:56 AM, emj83 wrote:
>
> Hi,
>
> I have several matrix in a list, for example:
> e
> [[1]]
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
>
> [[2]]
> [,1] [,2]
> [1,] 1 4
> [2,] 2 5
> [3,] 3 6
>
> [[3]]
> [,1] [,2]
> [1,] 2 1
>
> I would like to join them by column i.e.
> [,1] [,2] [,3] [,4][,5] [,6]
> [1,] 1 3 1 4 2 1
> [2,] 2 4 2 5 NA NA
> [3,] NA NA 3 6 NA NA
>
> I have tried do.call(cbind,e) but I get this error message as the rows are
> of different length
> Error in function (..., deparse.level = 1) :
> number of rows of matrices must match (see arg 2)
>
> Can anyone advise me please?
>
> Thanks Emma
>
>

Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Excellent that is just what I need. Thank you so much for your prompt help,
Emma


On Jan 6, 2011, at 6:23 AM, Dimitris Rizopoulos wrote:
> try this:
>
> matLis < list(matrix(1:4, 2, 2), matrix(1:6, 3, 2),
> matrix(2:1, 1, 2))
>
> n < max(sapply(matLis, nrow))
> do.call(cbind, lapply(matLis, function (x)
> rbind(x, matrix(, nnrow(x), ncol(x)))))
It's good that you solved the OP's question so neatly, since the
alternate solution I was going to propose turns out to be for a
different problem. Had the problem been for binding by row and padding
with NA's, there is a readymade function in the plyr package,
rbind.fill.matrix(). No cbind.fill or cbind.fill.matrix, yet. It looks
as though switching the roles of column and row in either of your
respective solutions could create a general solution though.

David.
>
>
> I hope it helps.
>
> Best,
> Dimitris
>
>
> On 1/6/2011 11:56 AM, emj83 wrote:
>>
>> Hi,
>>
>> I have several matrix in a list, for example:
>> e
>> [[1]]
>> [,1] [,2]
>> [1,] 1 3
>> [2,] 2 4
>>
>> [[2]]
>> [,1] [,2]
>> [1,] 1 4
>> [2,] 2 5
>> [3,] 3 6
>>
>> [[3]]
>> [,1] [,2]
>> [1,] 2 1
>>
>> I would like to join them by column i.e.
>> [,1] [,2] [,3] [,4][,5] [,6]
>> [1,] 1 3 1 4 2 1
>> [2,] 2 4 2 5 NA NA
>> [3,] NA NA 3 6 NA NA
>>
>> I have tried do.call(cbind,e) but I get this error message as the
>> rows are
>> of different length
>> Error in function (..., deparse.level = 1) :
>> number of rows of matrices must match (see arg 2)
>>
>> Can anyone advise me please?
>>
>> Thanks Emma
>>
>>
>
> 
> Dimitris Rizopoulos
> Assistant Professor
> Department of Biostatistics
> Erasmus University Medical Center
>
> Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
> Tel: +31/(0)10/7043478
> Fax: +31/(0)10/7043014
> Web: http://www.erasmusmc.nl/biostatistiek/>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
David Winsemius, MD
West Hartford, CT
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On Thu, Jan 6, 2011 at 5:56 AM, emj83 < [hidden email]> wrote:
>
> Hi,
>
> I have several matrix in a list, for example:
> e
> [[1]]
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
>
> [[2]]
> [,1] [,2]
> [1,] 1 4
> [2,] 2 5
> [3,] 3 6
>
> [[3]]
> [,1] [,2]
> [1,] 2 1
>
> I would like to join them by column i.e.
> [,1] [,2] [,3] [,4][,5] [,6]
> [1,] 1 3 1 4 2 1
> [2,] 2 4 2 5 NA NA
> [3,] NA NA 3 6 NA NA
>
> I have tried do.call(cbind,e) but I get this error message as the rows are
> of different length
> Error in function (..., deparse.level = 1) :
> number of rows of matrices must match (see arg 2)
>
One reasonably simple approach is to convert your matrices to time
series (either ts series or zoo series) as cbind.ts and cbind.zoo both
NA fill.
L < list(matrix(1:4, 2, 2), matrix(1:6, 3, 2), matrix(2:1, 1, 2))
# using ts
M < unclass(do.call(cbind, lapply(L, ts)))
tsp(M) < colnames(M) < NULL
# With zoo its slightly shorter:
library(zoo)
M < coredata(do.call(cbind, lapply(L, zoo)))
colnames(M) < NULL
We can omit the colnames(M) < NULL part in both cases if the list
itself or the constituent matrices have column names, e.g.
L < list(A = matrix(1:4, 2, 2), B = matrix(1:6, 3, 2), C = matrix(2:1, 1, 2))
# or
L < list(cbind(a = 1:2, b = 3:4), cbind(c = 1:3, d = 4:6), cbind(e = 2, f = 1))

Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1877GKXGROUP
email: ggrothendieck at gmail.com
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Dear Emma,
there is a 'cbind.na', 'rbind.na' and 'data.frame.na' function in my qpcR package.
library(qpcR)
matLis < list(matrix(1:4, 2, 2), matrix(1:6, 3, 2),
matrix(2:1, 1, 2))
do.call(cbind.na, matLis)
They are essentially the generic functions extended with an internal fill.
You might also want to try these examples:
## binding
cbind.na(1, 1:7) # the '1' (= shorter vector) is NOT recycled but filled
cbind.na(1:8, 1:7, 1:5, 1:10) # same with many vectors
rbind.na(1:8, 1:7, 1:5, 1:10) # or in rows
a < matrix(rnorm(20), ncol = 4) # unequal size matrices
b < matrix(rnorm(20), ncol = 5)
cbind.na(a, b) # works, in contrast to original cbind
rbind.na(a, b) # works, in contrast to original rbind
## data frame with unequal size vectors
data.frame.na(A = 1:7, B = 1:5, C = letters[1:3],
D = factor(c(1, 1, 2, 2)))
## convert a list with unequal length list items
## to a data frame
z < list(a = 1:5, b = letters[1:3], c = matrix(rnorm(20), ncol = 2))
do.call(data.frame.na, z)


Can this be used in matplot(x,y,....)? where x and y have matching rows. However, each column may have different rows or length. Thanks. Sheng


This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hello,
I have a similar question as emj83 but I have data frames as apposed to matrices. For example:
Dataset1:
Code Cap04
2 120
6 75
7 220
17 4
Dataset2:
Code Cap08
2 120
7 112
9 190
17 4
I need the dataset to look like the following where it keeps every unique 'code' from both datasets and fills the blanks in both rows with 0:
FinalDataSet:
Code Cap04 Cap08
2 120 120
6 75 0
7 220 112
9 0 190
17 4 4
Can anyone help me with this please?
Thank you very much!
Johnson

