[R] cbind/rbind and NULL

From: McGehee, Robert <Robert.McGehee_at_geodecapital.com>
Date: Wed 19 Oct 2005 - 09:04:36 EST


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              

>


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Oct 19 09:08:09 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:05:28 EST