检查R中的向量是否是连续的?
如何检查整数向量是否是“连续的”,即后续元素之间的差值恰好为一。我觉得我错过了像“is.sequential”这样的东西
这是我自己的功能:
is.sequential <- function(x){
all(diff(x) == rep(1,length(x)-1))
}
How can I check whether an integer vector is "sequential", i.e. that the difference between subsequent elements is exactly one. I feel like I am missing something like "is.sequential"
Here's my own function:
is.sequential <- function(x){
all(diff(x) == rep(1,length(x)-1))
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不需要
rep
因为 1 将被重新记录:编辑为允许 5:2 为 true
允许不同的序列
There's no need for
rep
since 1 will be recicled:Edited to allow 5:2 as true
To allow for diferent sequences
所以,@Iselzer 有一个很好的答案。但仍然存在一些极端情况:舍入误差和起始值。这是一个允许舍入错误但检查第一个值是否(几乎)是整数的版本。
So, @Iselzer has a fine answer. There are still some corner cases though: rounding errors and starting value. Here's a version that allows rounding errors but checks that the first value is (almost) an integer.
这个问题现在已经很老了,但在某些情况下,知道向量是否是顺序的实际上非常有用。
两个OP答案都很好,但正如汤米提到的,接受的答案有一些缺陷。 “序列”似乎是任何“等距的数字序列”,这似乎很自然。这包括负序列、起始值不同于 0 或 1 的序列,等等。
下面给出了一个非常多样化且安全的实现,它解释了
基本上,实现会检查差异的最大值和最小值是否完全相等。
虽然使用 S3 类方法使实现稍微复杂一些,但它简化了对错误输入类型的检查,并允许其他类的实现。例如,这使得扩展此方法来表示
Date
对象变得很简单,这需要考虑仅包含工作日(或工作日)的序列是否也是一个序列。速度比较
此实现非常安全,但使用 S4 类会增加一些开销。对于小长度向量,好处是实现的多样性,而最坏的情况下会慢 15% 左右。然而,对于较大的向量,它会稍微快一些,如下面的微基准测试所示。
请注意,中值时间更适合比较,因为垃圾清理器可能会为基准添加不确定的时间。
This question is quite old by now, but in certain circumstances it is actually quite useful to know whether a vector is sequential.
Both of the OP answers are quite good, but as mentioned by Tommy the accepted answer has some flaws. It seems natural that a 'sequence' is any 'sequence of numbers, which are equally spaced'. This would include negative sequences, sequences with a starting value outside different from 0 or 1, and so forth.
A very diverse and safe implementation is given below, which accounts for
Basically the implementation checks if the max and min of the differences are exactly equal.
While using the S3 class methods makes the implementation slightly more complicated it simplifies checks for wrong input types, and allows for implementations for other classes. For example this makes it simple to extend this method to say
Date
objects, which would require one to consider if a sequence of only weekdays (or work days) is also a sequence.Speed comparison
This implementation is very safe, but using S4 classes adds some overhead. For small length vectors the benefit is the diversity of the implementation, while it is around 15 % slower at worst. For larger vectors it is however slightly faster as shown in the microbenchmark below.
Note that the median time is better for comparison, as the garbage cleaner may add uncertain time to the benchmark.