Bug 15468 - rbind of list and matrix produces NULL's
rbind of list and matrix produces NULL's
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Language
R 3.0.1
All Mac OS X v10.8
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-09-21 04:41 UTC by Ricardo Saporta
Modified: 2013-09-24 23:13 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ricardo Saporta 2013-09-21 04:41:20 UTC
short:  rbind'ing a matrix with a list produces NULL values for matrix elements that were cropped. 

sample output: 
> rbind(M, L2)
     Col1      Col2     
Row1 11        12       
Row2 Numeric,2 Numeric,2
Row3 NULL      NULL     
L2   NULL      NULL     

------------------------------------------

A few  Examples:

M <- matrix(11:16, ncol=2, dimnames=list(paste0("Row", 1:3), paste0("Col", 1:2)))
M2 <- matrix(11:22, ncol=3, dimnames=list(paste0("Row", 1:4), paste0("Col", 1:3)))
L <- list(elem1=1, elem2=2)
L2 <- list(c(elem1=1, elem2=2))

rbind(M, L)
rbind(L, M)

rbind(M, L2)
rbind(L2, M)

rbind(L2, M)[1, ]

rbind(M2, L)
rbind(L, M2)

rbind(M2, L2)
rbind(L2, M2)

(output below, following sessionInfo() )
-----------------------------

sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

-----------------------------
> rbind(M, L)
     Col1 Col2
Row1 11   12  
Row2 1    2   
Row3 NULL NULL
L    NULL NULL
> rbind(L, M)
     elem1 elem2
L    1     2    
Row1 11    12   
Row2 NULL  NULL 
Row3 NULL  NULL 
> 
> rbind(M, L2)
     Col1      Col2     
Row1 11        12       
Row2 Numeric,2 Numeric,2
Row3 NULL      NULL     
L2   NULL      NULL     
> rbind(L2, M)
     Col1      Col2     
L2   Numeric,2 Numeric,2
Row1 11        12       
Row2 NULL      NULL     
Row3 NULL      NULL     
> 
> rbind(L2, M)[1, ]
$Col1
elem1 elem2 
    1     2 

$Col2
elem1 elem2 
    1     2 

> 
> rbind(M2, L)
     Col1 Col2 Col3
Row1 11   12   13  
Row2 1    2    1   
Row3 NULL NULL NULL
Row4 NULL NULL NULL
L    NULL NULL NULL
Warning message:
In rbind(M2, L) :
  number of columns of result is not a multiple of vector length (arg 2)
> rbind(L, M2)
     Col1 Col2 Col3
L    1    2    1   
Row1 11   12   13  
Row2 NULL NULL NULL
Row3 NULL NULL NULL
Row4 NULL NULL NULL
Warning message:
In rbind(L, M2) :
  number of columns of result is not a multiple of vector length (arg 1)
> 
> rbind(M2, L2)
     Col1      Col2      Col3     
Row1 11        12        13       
Row2 Numeric,2 Numeric,2 Numeric,2
Row3 NULL      NULL      NULL     
Row4 NULL      NULL      NULL     
L2   NULL      NULL      NULL     
> rbind(L2, M2)
     Col1      Col2      Col3     
L2   Numeric,2 Numeric,2 Numeric,2
Row1 11        12        13       
Row2 NULL      NULL      NULL     
Row3 NULL      NULL      NULL     
Row4 NULL      NULL      NULL
Comment 1 Duncan Murdoch 2013-09-24 21:43:35 UTC
The problem is that the matrix is coerced to a list.  When that happens it loses its dimensions, but the code in bind.c assumes they are still there.

I will work on fixing this.
Comment 2 Duncan Murdoch 2013-09-24 23:13:24 UTC
Fixed in R-devel, will fix in R-patched after 3.0.2 is released