創建一個空的data.frame

本文翻譯自:Create an empty data.frame

I'm trying to initialize a data.frame without any rows. 我正在嘗試初始化沒有任何行的data.frame。 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. 這將創建一個data.frame,其中包含包含我想要的所有數據類型和列名的單行,而且還會創建一個無用的行,然後將其刪除。

Is there a better way to do this? 有一個更好的方法嗎?


#1樓

參考:https://stackoom.com/question/iqi7/創建一個空的data-frame


#2樓

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使用。


#3樓

You can do it without specifying column types 您可以在不指定列類型的情況下進行操作

df = data.frame(matrix(vector(), 0, 3,
                dimnames=list(c(), c("Date", "File", "User"))),
                stringsAsFactors=F)

#4樓

You could use read.table with an empty string for the input text as follows: 您可以將read.table與空字符串一起用於輸入text ,如下所示:

colClasses = c("Date", "character", "character")
col.names = c("Date", "File", "User")

df <- read.table(text = "",
                 colClasses = colClasses,
                 col.names = col.names)

Alternatively specifying the col.names as a string: 或者將col.names指定爲字符串:

df <- read.csv(text="Date,File,User", colClasses = colClasses)

Thanks to Richard Scriven for the improvement 感謝Richard Scriven的改進


#5樓

If you are looking for shortness : 如果您正在尋找短缺:

read.csv(text="col1,col2")

so you don't need to specify the column names separately. 因此您無需單獨指定列名稱。 You get the default column type logical until you fill the data frame. 在填充數據框之前,您將獲得默認的邏輯列類型。


#6樓

The most efficient way to do this is to use structure to create a list that has the class "data.frame" : 實現此目的的最有效方法是使用structure創建具有"data.frame"類的列表:

structure(list(Date = as.Date(character()), File = character(), User = character()), 
          class = "data.frame")
# [1] Date File User
# <0 rows> (or 0-length row.names)

To put this into perspective compared to the presently accepted answer, here's a simple benchmark: 與當前接受的答案相比,這是一個簡單的基準:

s <- function() structure(list(Date = as.Date(character()), 
                               File = character(), 
                               User = character()), 
                          class = "data.frame")
d <- function() data.frame(Date = as.Date(character()),
                           File = character(), 
                           User = character(), 
                           stringsAsFactors = FALSE) 
library("microbenchmark")
microbenchmark(s(), d())
# Unit: microseconds
#  expr     min       lq     mean   median      uq      max neval
#   s()  58.503  66.5860  90.7682  82.1735 101.803  469.560   100
#   d() 370.644 382.5755 523.3397 420.1025 604.654 1565.711   100
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章