R: Best way to replicate an object of same size and type as other?

半世苍凉 提交于 2019-12-23 05:13:59

问题


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.

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?


回答1:


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 ...


来源:https://stackoverflow.com/questions/41605723/r-best-way-to-replicate-an-object-of-same-size-and-type-as-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!