连续整数运行 [英] Continuous integer runs
问题描述
rle
,但不知道如何在这里使用)。 更容易查看数据集并解释我之后的情况。
这里是数据视图:
$ greg
[1] 7 8 9 10 11 20 21 22 23 24 30 31 32 33 49
$研究员
[1] 42 43 44 45 46 47 48
$ sally
[1] 25 26 27 28 29 37 38 39 40 41
$ sam
[1] 1 2 3 4 5 6 16 17 18 19 34 35 36
$ teacher
[1] 12 13 14 15
期望的输出:
$ greg
[1] 7:11,20:24,30:33,49
$研究员
[1] 42:48
$ sally
[1] 25:29,37:41
$ sam
[1] 1:6,16:19 34:36
$ teacher
[1] 12:15
使用基本包,如何用连字符替换连续span en最高和最低和非连续部分之间的逗号?请注意,数据从整数向量列表到字符向量列表。
MWE数据
z < - structure(list(greg = c(7L,8L,9L,10L,11L,20L,21L,22L,
23L ,24L,30L,31L,32L,33L,49L),研究员= 42:48,sally = c(25L,
26L,27L,28L,29L,37L,38L,39L,40L,41L) = c(1L,2L,
3L,4L,5L,6L,16L,17L,18L,19L,34L,35L,36L),teacher = 12:15),Names = c(greg
研究员,sally,sam,老师))
我认为 diff
是解决方案。您可能需要一些额外的费用来处理单身人士,但是:
lapply(z,function(x){
diffs start_indexes < - c(1,which(diffs> 1))
end_indexes< - c(start_indexes - 1,length x))
coloned< - paste(x [start_indexes],x [end_indexes],sep =:)
paste0(coloned,collapse =,)
})
$ greg
[1]7:11,20:24,30:33,49:49
$研究员
[1] 42:48
$ sally
[1]25:29,37:41
$ sam
[1]1 :6,16:19,34:36
$ teacher
[1]12:15
I have some data in a list that I need to look for continuous runs of integers (My brain thinkrle
but don't know how to use it here).
It's easier to look at the data set and explain what I'm after.
Here's the data view:
$greg
[1] 7 8 9 10 11 20 21 22 23 24 30 31 32 33 49
$researcher
[1] 42 43 44 45 46 47 48
$sally
[1] 25 26 27 28 29 37 38 39 40 41
$sam
[1] 1 2 3 4 5 6 16 17 18 19 34 35 36
$teacher
[1] 12 13 14 15
Desired output:
$greg
[1] 7:11, 20:24, 30:33, 49
$researcher
[1] 42:48
$sally
[1] 25:29, 37:41
$sam
[1] 1:6, 16:19 34:36
$teacher
[1] 12:15
Use base packages how can I replace continuous span with a colon between highest and lowest and commas in between non the non continuous parts? Note that the data goes from a list of integer vectors to a list of character vectors.
MWE data:
z <- structure(list(greg = c(7L, 8L, 9L, 10L, 11L, 20L, 21L, 22L,
23L, 24L, 30L, 31L, 32L, 33L, 49L), researcher = 42:48, sally = c(25L,
26L, 27L, 28L, 29L, 37L, 38L, 39L, 40L, 41L), sam = c(1L, 2L,
3L, 4L, 5L, 6L, 16L, 17L, 18L, 19L, 34L, 35L, 36L), teacher = 12:15), .Names = c("greg",
"researcher", "sally", "sam", "teacher"))
I think diff
is the solution. You might need some additional fiddling to deal with the singletons, but:
lapply(z, function(x) {
diffs <- c(1, diff(x))
start_indexes <- c(1, which(diffs > 1))
end_indexes <- c(start_indexes - 1, length(x))
coloned <- paste(x[start_indexes], x[end_indexes], sep=":")
paste0(coloned, collapse=", ")
})
$greg
[1] "7:11, 20:24, 30:33, 49:49"
$researcher
[1] "42:48"
$sally
[1] "25:29, 37:41"
$sam
[1] "1:6, 16:19, 34:36"
$teacher
[1] "12:15"
这篇关于连续整数运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!