[R] cbind/rbind and NULL

McGehee, Robert Robert.McGehee at geodecapital.com
Wed Oct 19 01:04:36 CEST 2005


I'm trying to understand why I can rbind but not cbind dataframes to
NULLs.

	For 'cbind' ('rbind'), vectors of zero length (including 'NULL')
     are ignored unless the result would have zero rows (columns),  for
     S compatibility. (Zero-extent matrices do not occur in S3 and are
     not ignored in R.)

Since NULL is a vector of length 0, should it not be ignored for both
cbind and rbind? If not, then shouldn't it fail for the same reason for
both functions?

> data(USArrests)
> rbind(USArrests, NULL)
               Murder Assault UrbanPop Rape
Alabama          13.2     236       58 21.2
Alaska           10.0     263       48 44.5
...

> cbind(USarrests, NULL)
Error in data.frame(..., check.names = FALSE) : 
        arguments imply differing number of rows: 50, 0

Also, cbinding vectors works:
> cbind(USArrests[,1], NULL)
      [,1]
 [1,] 13.2
 [2,] 10.0
 [3,]  8.1

But not if USArrests is a dataframe:

> cbind(USArrests[,1, drop = FALSE], NULL)
Error in data.frame(..., check.names = FALSE) : 
        arguments imply differing number of rows: 50, 0

Thanks, 
Robert


> version
         _              
platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    2              
minor    2.0            
year     2005           
month    10             
day      06             
svn rev  35749          
language R              
>




More information about the R-help mailing list