這裏是一個黑客建立無行空的數據幀,並沒有列:初始化一個完整的空數據幀(沒有行,沒有列)
iris[FALSE, FALSE]
#> data frame with 0 columns and 0 rows
智慧的前瞻性代碼創建一個虛假的列:
x <- list(NULL)
class(x) <- c("data.frame")
attr(x, "row.names") <- integer(0)
str(x)
#> 'data.frame': 0 obs. of 1 variable:
#> $ : NULL
有沒有非黑客選擇?
創建這樣一個事情的原因是爲了滿足可以處理空數據幀但不是NULL的函數。
這不同於類似的問題,因爲它是關於沒有列以及沒有行。
但是,問題是有關指定列類型。 – nacnudus
'structure(list(),class =「data.frame」)'將會成爲你嘗試添加一個類到列表的原始方法。 – thelatemail
我不認爲這是重複的 – thelatemail