[英]Initializing a blank data.frame with the same column and row names as existing data.frame
[英]How do I create a new data.frame with the same column names and types as an existing data.frame?
我有一个要克隆的data.frame,但是其中没有值。 即。 我希望新框架具有与现有框架相同的列名称和类型。 原始框架的大小约为GB,因此复制和删除数据似乎不是正确的方法,并且对现有列进行迭代以将其添加到新框架似乎不是一个很好的“ R'做事的方式。
如果要零行,请参考第零行:
iris[0,]
[1] Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<0 rows> (or 0-length row.names)
sapply(iris[0,],class) # still has correct column classes
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
"numeric" "numeric" "numeric" "numeric" "factor"
如果需要NA数据,请在原始数据的行域之外进行引用:
iris[nrow(iris)+1:3,]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
NA NA NA NA NA <NA>
NA.1 NA NA NA NA <NA>
NA.2 NA NA NA NA <NA>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.