通常禁用矩阵的降维? [英] Generally disable dimension dropping for matrices?

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

问题描述

众所周知,如果 R 的列维或行维为 1,它会尝试将矩阵简化为向量.使用 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.

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

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