Just initialize it with empty vectors:
(只需使用空向量对其进行初始化:)
df <- data.frame(Date=as.Date(character()),
File=character(),
User=character(),
stringsAsFactors=FALSE)
Here's an other example with different column types :
(这是另一个具有不同列类型的示例:)
df <- data.frame(Doubles=double(),
Ints=integer(),
Factors=factor(),
Logicals=logical(),
Characters=character(),
stringsAsFactors=FALSE)
str(df)
> str(df)
'data.frame': 0 obs. of 5 variables:
$ Doubles : num
$ Ints : int
$ Factors : Factor w/ 0 levels:
$ Logicals : logi
$ Characters: chr
NB :
(注意:)
Initializing a data.frame
with an empty column of the wrong type does not prevent further additions of rows having columns of different types.
(使用错误类型的空列初始化data.frame
不会阻止进一步添加具有不同类型列的行。)
This method is just a bit safer in the sense that you'll have the correct column types from the beginning, hence if your code relies on some column type checking, it will work even with a data.frame
with zero rows.
(从一开始就具有正确的列类型的意义上说,此方法稍微安全一些,因此,如果您的代码依赖于某些列类型检查,则即使行数为零的data.frame
使用。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…