[R] Adding a column to an **empty** data.frame

Emmanuel Levy emmanuel.levy at gmail.com
Wed Nov 2 14:48:05 CET 2016


Dear All,

This sounds simple but can't figure out a good way to do it.

Let's say that I have an empty data frame "df":

## creates the df
df = data.frame( id=1, data=2)

## empties the df, perhaps there is a more elegant way to create an empty
df?
df = df[-c(1),]

> df
[1] id   data
<0 rows> (or 0-length row.names)

Now, how can I add a third column name to that empty df?

Normally I would use df$new.col = 1

But here I can't use this because it's empty!

I tried df$new.col=NULL -- this doesn't give an error but doesn't add the
column.

Thanks for your help,

Emmanuel

	[[alternative HTML version deleted]]



More information about the R-help mailing list