本文介绍了通常禁用矩阵降维?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,如果R的列维或行维为1,则R尝试将矩阵简化为向量.可以使用drop=F命令来防止尺寸的自动下降.

As we all know, R tries to reduce matrices to vectors if its column- or row-dimension is 1. This automatic dropping of dimensions can be prevented by use of the drop=F command.

但是,我目前正在编写一个大型R包,并且需要在代码中几百次禁用降维功能,因此我将不得不手动查找这些位置并添加drop=F数百次.

However, I am currently writing a large R package and would require to disable dimension dropping on several hundred occasions in my code, so that I would have to manually find these locations and add drop=F many hundred times.

因此,我想知道是否有任何选择或可能性通常禁用R中矩阵的降维?

Therefore, I would like to know if there's any option or possibility to generally disable dimension dropping for matrices in R?

推荐答案

您可以通过重新定义[函数来实现:

You can do it by redefining the [ function:

x <- matrix(1:4,2)

`[` <- function(...) base::`[`(...,drop=FALSE)
x[,1]
     [,1]
[1,]    1
[2,]    2

尽管现在您不能在调用drop参数时覆盖它,所以您可能要少用它并在完成后删除它.

You cannot override the drop argument when you call it now though, so you might want to use it sparingly and delete when done.

这篇关于通常禁用矩阵降维?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:17