如何确定R中的数字是否连续? [英] How to find if the numbers are continuous in R?

查看:45
本文介绍了如何确定R中的数字是否连续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列值

c(1,2,3,4,5,8,9,10,13,14,15)

我想找到数字变得不连续的范围.我想要的只是作为输出:

And I want to find the ranges where the numbers become discontinuous. All I want is this as output:

(1,5)
(8,10)
(13,15)

我需要找到断点.

我需要用 R 来做.

推荐答案

类似的事情?

x <- c(1:5, 8:10, 13:15) # example data
unname(tapply(x, cumsum(c(1, diff(x)) != 1), range)
# [[1]]
# [1] 1 5
# 
# [[2]]
# [1]  8 10
# 
# [[3]]
# [1] 13 15

另一个例子:

x <- c(1, 5, 10, 11:14, 20:21, 23)
unname(tapply(x, cumsum(c(1, diff(x)) != 1), range))
# [[1]]
# [1] 1 1
#
# [[2]]
# [1] 5 5
#
# [[3]]
# [1] 10 14
#
# [[4]]
# [1] 20 21
#
# [[5]]
# [1] 23 23

这篇关于如何确定R中的数字是否连续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆