在R中,负索引有什么作用?
我正在将程序的一部分(不足以编译和运行)从 R 移植到 C++。我对 R 不熟悉。我已经很好地使用了在线参考资料,但被以下行难住了:
cnt2.2<-cnt2[,-1]
我猜测:
cnt2
是一个二维矩阵cnt2.2
是一个用句点“.”声明的新变量使用方式与字母字符相同。<-
是一个赋值。[,-1]
访问数组的一部分。我认为[,5]
表示所有行,仅第五列。如果这是正确的,我不知道-1指的是什么。
I am porting part of a program (not enough to compile and run) from R to C++. I am not familiar with R. I have done okay using the references online, but was stumped by the following line:
cnt2.2<-cnt2[,-1]
I am guessing:
cnt2
is a 2 dimensional matrixcnt2.2
is a new variable being declared with a period '.' used the same way an alphabetic character would be.<-
is an assignment.[,-1]
accesses part of the array. I thought[,5]
meant all rows, 5th column only. If this is correct, I have no idea what -1 refers to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
本手册第 2.7 节对此进行了介绍:http:// /cran.r-project.org/doc/manuals/R-intro.html#Index-vectors
它是
cnt2
对象的负索引,指定除第一栏。This is covered in section 2.7 of the manual: http://cran.r-project.org/doc/manuals/R-intro.html#Index-vectors
It is a negative index into the
cnt2
object specifying all rows and all columns except the first column.负索引指定删除(而不是保留)特定元素...因此
x[,-1]
指定删除第一列(行是逗号之前的第一个维度,列是第二个维度) ,在逗号之后)。来自?"["
( http://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.html):Negative indices specify dropping (rather than retaining) particular elements ... so
x[,-1]
specifies dropping the first column (rows are the first dimension, before the comma, and columns are the second dimension, after the comma). From?"["
( http://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.html ):从您提供的代码来看,它确实是某种二维结构(很可能是一个矩阵)。
正确的。
正确的。
[,-1]
选择除第 1 列之外的所有列。请注意,与 C++ 不同,R 中的索引从 1 开始,而不是从 0 开始。From the code you provided it is indeed a 2-dimensional structure of some sort (quite possibly a matrix).
Correct.
Correct.
[,-1]
selects all columns except column 1. Note that, unlike in C++, indices in R start from one rather than zero.