我有一些數據在列表中,我需要尋找連續運行的整數(我的大腦認爲rle
,但不知道如何在這裏使用它)。連續整數運行
查看數據集並解釋我後面的內容會更容易。
這裏的數據視圖:
$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
所需的輸出:
$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
使用基礎包我怎麼能在兩者之間最高和最低和逗號之間的結腸代替連續跨非非連續部分?請注意,數據從整數向量列表轉換爲字符向量列表。
MWE數據:
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"))
你的問題是類似位這一個:http://stackoverflow.com/q/7077710/602276 – Andrie 2013-02-14 06:03:06