[R] Data frame with 0 rows. (Thanks.)

Rolf Turner r.turner at auckland.ac.nz
Wed Feb 20 20:31:00 CET 2008


Thanks to Bill Venables, Gabor Grothendieck, and Tony Plate
all of whom gave me useful advice in answer to my question:

>> For reasons best known only to myself ( :-) ) I wish to create a data
>> frame with 0 rows and 9 columns.
>> The best I've been able to come up with is:
>> 	junk <- as.data.frame(matrix(0,nrow=0,ncol=9))
>> Is there a sexier way?

My personal preference among the suggested solutions was Tony Plate's:

> > as.data.frame(rep(list(a=numeric(0)), 9))
> [1] a   a.1 a.2 a.3 a.4 a.5 a.6 a.7 a.8
> <0 rows> (or 0-length row.names)

Thanks again to all.

	cheers,

		Rolf Turner

P. S. It is interesting to observe that if you rbind() such a zero  
row data frame
onto another one (with the same column names of course) then the type/ 
mode of the columns
in the result is determined by the second (``non-empty'') data  
frame.  It appears
that if you rbind two non-empty data frames, the type/mode of the  
columns of the
result is determined by coercion to a sort of minimal type --- at  
least rbind()-ing
numeric to factor yields character.  I haven't experimented  
thoroughly (nor delved
into the source) but it would seem that the designers have very  
cleverly made the
result of rbinding as sensible as it is possible to be, given the  
silly behaviour
of the user!

		R. T.

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list