It is easy to create a empty data frame, but some how tricky to create a empty data frame with columns defined. To create a simple empty data frame you do:
data_frame < - data.frame()
However no if you want to assign a value to a particular column in your data frame your first need to define column names. For that you can do:
data_frame < - data.frame(t(rep(NA,number_of_columns)))
//you can change number_of_columns according to your need
names(data_frame) <- c("column1","column2","column3","column4","column5","column6")
//Assign column names (i have assumed number_of_columns as 6)
data_frame <- data_frame[-1,]
//Remove all NAs
So now the data frame that you have is:
> data_frame [1] column1 column2 column3 column4 column5 column6 <0 rows> (or 0-length row.names)
August 18, 2011 at 12:26 pm
This is great!. sometimes you need to create empty dataframes..
August 25, 2011 at 6:52 pm
That’s very useful. I’ve wanted to do this a lot, and this is the slickest way round the irritation.
April 6, 2012 at 1:04 pm
fine ! the simplest way to do this !