Quantcast

How to join matrices of different row length from a list

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to join matrices of different row length from a list

emj83
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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to join matrices of different row length from a list

D. Rizopoulos
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(, n-nrow(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/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to join matrices of different row length from a list

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

Emma
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to join matrices of different row length from a list

David Winsemius
In reply to this post by D. Rizopoulos

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(, n-nrow(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 ready-made 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/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to join matrices of different row length from a list

Gabor Grothendieck
In reply to this post by emj83
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: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to join matrices of different row length from a list

A.N. Spiess
In reply to this post by emj83
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)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to join matrices of different row length from a list

Sheng Lin
In reply to this post by D. Rizopoulos
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to join matrices of different row length from a list

Jeff Newmiller
No.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<[hidden email]> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

Sheng Lin <[hidden email]> wrote:

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

--
View this message in context: http://r.789695.n4.nabble.com/How-to-join-matrices-of-different-row-length-from-a-list-tp3177212p3607158.html
Sent from the R help mailing list archive at Nabble.com.

_____________________________________________

[hidden email] 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.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to join data.frames of different row length from a list

Johnson
This post has NOT been accepted by the mailing list yet.
This post was updated on .
In reply to this post by emj83
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

Loading...