[R] how to construct an empty data.frame

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Apr 28 19:38:17 CEST 2005


On Thu, 28 Apr 2005, Omar Lakkis wrote:

>> r
> [1] open   settle
> <0 rows> (or 0-length row.names)
>> class(r)
> [1] "data.frame"
>
> this is an empty data.frame I get back from a sql statement that
> returns an empty result set. How can I create such an empty data.frame
> using the data.frame() constructor?
> I want to have a data.frame with 0 rows but named empty columns.

Is this what you want?

> x <- data.frame(a=character(0), b=numeric(0))
> x
[1] a b
<0 rows> (or 0-length row.names)
> names(x)
[1] "a" "b"



-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list