简体   繁体   English

使用冒号运算符了解数据帧的索引

[英]Understanding indexing of data frame with colon operator

I have a data.frame that is from the predictive maintenance R Notebook example by MS. 我有一个来自MS的预测性维护R Notebook示例的data.frame。

Now they show how to subset this data.frame like this (showing some of the first rows and some of the last with just one line of code instad of using head() or tail() ): 现在,他们展示了如何像这样子化data.frame的子集(显示了使用head()tail()只显示一行代码的前几行和最后几行):

> errors[c(1:3, nrow(errors)-3:1),]
                datetime machineID errorID
1    2015-01-03 07:00:00         1  error1
2    2015-01-03 20:00:00         1  error3
3    2015-01-04 06:00:00         1  error5
3916 2015-12-04 02:00:00       100  error1
3917 2015-12-08 06:00:00       100  error2
3918 2015-12-08 06:00:00       100  error3

They want to output 他们想输出

I do unterstand what the colon operator does. 我不理解冒号运算符的作用。 But I do not really understand what it does in this example. 但是我并不真正理解此示例中的功能。 Because: while 4:1 will return 因为:而4:1将返回

> 4:1
[1] 4 3 2 1

and

nrow(errors):1
   [1] 3919 3918 3917 3916 3915 3914 3913 3912 3911 3910 3909 3908 3907 3906 3905 3904 3903
  [18] 3902 3901 3900 3899 3898 3897 3896 3895 3894 3893 3892 3891 3890 3889 3888 3887 3886
...

Then the following does not return what I would expect: 然后以下内容不会返回我期望的结果:

> nrow(errors)-3:1
[1] 3916 3917 3918

I would have expected that it returns the same long list as before, but starting with the index at nrow(errors)-3 . 我本来希望它返回与以前相同的长列表,但是从nrow(errors)-3处的索引开始。 So something like: 所以像这样:

nrow(errors):1
       [1] 3916 3915 3914 3913 3912 3911 3910 3909 3908 3907 3906 3905 3904 3903
    ...

What do I understand wrong here? 我在这里理解错了什么? Thanks in advance! 提前致谢!

Thanks to @markus and @Aaron Hayman and @G Grothendieck 感谢@markus和@Aaron Hayman和@G Grothendieck

The colon operator is evaluated first. 首先评估冒号运算符。 So that 以便

> 3:1
[1] 3 2 1

And nrow(errors) will return 3919 . 并且nrow(errors)将返回3919 Then subtracting 3:1 will give a vector like c(3919-3, 3919-2, 3919-1) 然后减去3:1将得到一个像c(3919-3, 3919-2, 3919-1)的向量

And by thinking this over again, I realize it should be: 通过重新考虑这一点,我意识到应该是:

> nrow(errors)-2:0
[1] 3917 3918 3919

to really get the last three lines, like in the following: 真正获得最后三行,如下所示:

> errors[c(1:3, nrow(errors)-2:0),]
                datetime machineID errorID
1    2015-01-03 07:00:00         1  error1
2    2015-01-03 20:00:00         1  error3
3    2015-01-04 06:00:00         1  error5
3917 2015-12-08 06:00:00       100  error2
3918 2015-12-08 06:00:00       100  error3
3919 2015-12-22 03:00:00       100  error3

This helped understanding. 这有助于理解。 Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM