检查 R 中的向量是否是顺序的? [英] Check whether vector in R is sequential?
本文介绍了检查 R 中的向量是否是顺序的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查整数向量是否是连续的",即后续元素之间的差值恰好为 1.我觉得我缺少诸如is.sequential"之类的东西
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"
这是我自己的函数:
is.sequential <- function(x){
all(diff(x) == rep(1,length(x)-1))
}
推荐答案
不需要 rep
因为 1 将被重新定义:
There's no need for rep
since 1 will be recicled:
编辑以允许 5:2 为真
Edited to allow 5:2 as true
is.sequential <- function(x){
all(abs(diff(x)) == 1)
}
允许不同的序列
is.sequential <- function(x){
all(diff(x) == diff(x)[1])
}
这篇关于检查 R 中的向量是否是顺序的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文