How to create empty data frame with column names specified in R?

RDataframe

R Problem Overview


> Possible Duplicate:
> Create an Empty Data.Frame

I need to create an empty data frame in R with specified column names. Any simplest way ?

R Solutions


Solution 1 - R

Just create a data.frame with 0 length variables

eg

nodata <- data.frame(x= numeric(0), y= integer(0), z = character(0))
str(nodata)

## 'data.frame':	0 obs. of  3 variables:
##  $ x: num 
##  $ y: int 
##  $ z: Factor w/ 0 levels: 

or to create a data.frame with 5 columns named a,b,c,d,e

nodata <- as.data.frame(setNames(replicate(5,numeric(0), simplify = F), letters[1:5]))

Solution 2 - R

Perhaps:

> data.frame(aname=NA, bname=NA)[numeric(0), ]
[1] aname bname
<0 rows> (or 0-length row.names)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSurjya Narayana PadhiView Question on Stackoverflow
Solution 1 - RmnelView Answer on Stackoverflow
Solution 2 - RIRTFMView Answer on Stackoverflow