R:复制大小和类型与其他对象相同的对象的最佳方法? [英] R: Best way to replicate an object of same size and type as other?
本文介绍了R:复制大小和类型与其他对象相同的对象的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设您有一些R对象,例如data.frame和Quanteda DFM稀疏矩阵.您要复制相同大小的对象,但无需复制内容.
Suppose you have some R object, such data.frame and Quanteda DFM sparse matrix. You want to replicate that object of the same size but no need to copy the content.
是否有一些R命令可以复制任何对象而不复制内容?如果是,它们是否可以处理稀疏对象和非稀疏对象?
Is there some R command to replicate any object without copying the content? And if yes, do they work over sparse objects and non-sparse objects?
推荐答案
这将创建相同的数据结构,并用NA
this will create the same data structure filled with NA
data("iris")
iris.mt <- iris[0, ]
iris.mt[nrow(iris), ] <- NA
str(iris.mt)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num NA NA NA NA NA NA NA NA NA NA ...
$ Sepal.Width : num NA NA NA NA NA NA NA NA NA NA ...
$ Petal.Length: num NA NA NA NA NA NA NA NA NA NA ...
$ Petal.Width : num NA NA NA NA NA NA NA NA NA NA ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: NA NA NA NA NA NA NA NA NA NA ...
这篇关于R:复制大小和类型与其他对象相同的对象的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文