Create an empty data.frame

2018-12-31 23:42发布

I'm trying to initialize a data.frame without any rows. Basically, I want to specify the data types for each column and name them, but not have any rows created as a result.

The best I've been able to do so far is something like:

df <- data.frame(Date=as.Date("01/01/2000", format="%m/%d/%Y"), 
                 File="", User="", stringsAsFactors=FALSE)
df <- df[-1,]

Which creates a data.frame with a single row containing all of the data types and column names I wanted, but also creates a useless row which then needs to be removed.

Is there a better way to do this?

14条回答
还给你的自由
2楼-- · 2019-01-01 00:11

You can do it without specifying column types

df = data.frame(matrix(vector(), 0, 3,
                dimnames=list(c(), c("Date", "File", "User"))),
                stringsAsFactors=F)
查看更多
谁念西风独自凉
3楼-- · 2019-01-01 00:11

Just declare

table = data.frame()

when you try to rbind the first line it will create the columns

查看更多
登录 后发表回答