在R中,负索引有什么作用?

发布于 2024-12-03 12:58:13 字数 366 浏览 4 评论 0原文

我正在将程序的一部分(不足以编译和运行)从 R 移植到 C++。我对 R 不熟悉。我已经很好地使用了在线参考资料,但被以下行难住了:

cnt2.2<-cnt2[,-1]

我猜测:

  1. cnt2 是一个二维矩阵
  2. cnt2.2 是一个用句点“.”声明的新变量使用方式与字母字符相同。
  3. <- 是一个赋值。
  4. [,-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:

  1. cnt2 is a 2 dimensional matrix
  2. cnt2.2 is a new variable being declared with a period '.' used the same way an alphabetic character would be.
  3. <- is an assignment.
  4. [,-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆里有你的影子 2024-12-10 12:58:13

本手册第 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.

久隐师 2024-12-10 12:58:13

负索引指定删除(而不是保留)特定元素...因此 x[,-1] 指定删除第一列(行是逗号之前的第一个维度,列是第二个维度) ,在逗号之后)。来自 ?"[" ( http://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.html):

仅对于“[”索引:“i”、“j”、“...”可以是逻辑的
向量,指示要选择的元素/切片。这样的载体
如果需要的话会被回收以匹配相应的范围。
'i', 'j', '...' 也可以是负整数,表示
要从选择中排除的元素/切片。

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 ):

For ‘[’-indexing only: ‘i’, ‘j’, ‘...’ can be logical
vectors, indicating elements/slices to select. Such vectors
are recycled if necessary to match the corresponding extent.
‘i’, ‘j’, ‘...’ can also be negative integers, indicating
elements/slices to leave out of the selection.

2024-12-10 12:58:13

1) cnt2 是一个二维矩阵

从您提供的代码来看,它确实是某种二维结构(很可能是一个矩阵)。

2) cnt2.2 是一个用句点“.”声明的新变量使用方式与字母字符相同。

正确的。

3) <- 是一项作业。

正确的。

4) [,-1] 访问数组的一部分。我认为 [,5] 意味着所有行,仅第五列。如果这是正确的,我不知道 -1 指的是什么。

[,-1] 选择除第 1 列之外的所有列。请注意,与 C++ 不同,R 中的索引从 1 开始,而不是从 0 开始。

1) cnt2 is a 2 dimensional matrix

From the code you provided it is indeed a 2-dimensional structure of some sort (quite possibly a matrix).

2) cnt2.2 is a new variable being declared with a period '.' used the same way an alphabetic character would be.

Correct.

3) <- is an assignment.

Correct.

4) [,-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.

[,-1] selects all columns except column 1. Note that, unlike in C++, indices in R start from one rather than zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文