在R,NULL与NA中分配矩阵的最佳方法? [英] Best way to allocate matrix in R, NULL vs NA?

查看:80
本文介绍了在R,NULL与NA中分配矩阵的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写R代码来创建一个方矩阵.所以我的方法是:

I am writing R code to create a square matrix. So my approach is:

  1. 分配正确大小的矩阵
  2. 遍历矩阵的每个元素,并用适当的值填充

我的问题真的很简单:预分配此矩阵的最佳方法是什么?到目前为止,我有两种方法:

My question is really simple: what is the best way to pre-allocate this matrix? Thus far, I have two ways:

> x <- matrix(data=NA,nrow=3,ncol=3)
> x
     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA
[3,]   NA   NA   NA

> x <- list()
> length(x) <- 3^2
> dim(x) <- c(3,3)
> x
     [,1] [,2] [,3]
[1,] NULL NULL NULL
[2,] NULL NULL NULL
[3,] NULL NULL NULL

据我所知,前者是一种比后者更简洁的方法.同样,前者用NA填充矩阵,而后者用NULL填充.

As far as I can see, the former is a more concise method than the latter. Also, the former fills the matrix with NAs, whereas the latter is filled with NULLs.

做到这一点的更好"方法是什么?在这种情况下,我将更好"定义为更好的性能",因为这是统计计算,并且此操作将针对大型数据集进行.

Which is the "better" way to do this? In this case, I'm defining "better" as "better performance", because this is statistical computing and this operation will be taking place with large datasets.

虽然前者更为简洁,但理解起来却并非如此轻松,因此我觉得这可能会发生任何一种变化.

While the former is more concise, it isn't breathtakingly easier to understand, so I feel like this could go either way.

此外,R中的NA和NULL有什么区别? ?NA和?NULL告诉我"NA"的长度为"1",而NULL的长度为"0"-但是这里还有更多吗?还是最佳做法?这将影响我使用哪种方法创建矩阵.

Also, what is the difference between NA and NULL in R? ?NA and ?NULL tell me that "NA" has a length of "1" whereas NULL has a length of "0" - but is there more here? Or a best practice? This will affect which method I use to create my matrix.

推荐答案

如有疑问,请进行自我测试.第一种方法既简单又快速.

When in doubt, test yourself. The first approach is both easier and faster.

> create.matrix <- function(size) {
+ x <- matrix()
+ length(x) <- size^2
+ dim(x) <- c(size,size)
+ x
+ }
> 
> system.time(x <- matrix(data=NA,nrow=10000,ncol=10000))
   user  system elapsed 
   4.59    0.23    4.84 
> system.time(y <- create.matrix(size=10000))
   user  system elapsed 
   0.59    0.97   15.81 
> identical(x,y)
[1] TRUE

关于NA和NULL之间的区别:

Regarding the difference between NA and NULL:

实际上有四个特殊常数.

There are actually four special constants.

此外,还有四个特殊常量NULL,NA,Inf和NaN.

In addition, there are four special constants, NULL, NA, Inf, and NaN.

NULL用于指示空对象. NA用于缺少(不可用")数据值. Inf表示无穷大,而NaN在IEEE浮点演算中不是一个数字(例如,运算结果分别为1/0和0/0).

NULL is used to indicate the empty object. NA is used for absent ("Not Available") data values. Inf denotes infinity and NaN is not-a-number in the IEEE floating point calculus (results of the operations respectively 1/0 and 0/0, for instance).

您可以在有关语言的R手册中阅读更多内容定义.

这篇关于在R,NULL与NA中分配矩阵的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆