呈现如下矢量,
vec01 <- c(1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 1, 2, 1,
2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 2, 2,
1, 2, 2, 1, 1, 2, 3, 4, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 1, 1, 1,
2, 1, 2, 3, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1)
问题1:如何删除下面突出显示的异常:
vec01 <- c(1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 1, 2, 1,
2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, *2*, *2*,
1, 2, *2*, 1, 1, 2, 3, 4, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 1, 1, 1,
2, 1, 2, 3, 4, *2*, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1)
异常的定义:该元素需要属于系列 1,2,.... 上面以粗体标记的元素
问题2:如何在去除异常后识别序列组,每个序列属于一个组,即输出如下
result <- structure(list(vec = c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 2L, 1L, 2L),
group = c(1L, 1L, 2L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 10L, 10L, 11L,
11L, 12L, 13L, 14L, 15L, 15L, 16L, 16L)),
.Names = c("vec", "group"),
row.names = c(NA, 30L), class = "data.frame")