gpt4 book ai didi

r - 在组中的 data.table 字段中识别由 n 个连续数字组成的组

转载 作者:行者123 更新时间:2023-12-03 18:17:22 25 4
gpt4 key购买 nike

此数据表显示了学生参加的一年中的月份。

DT = data.table(
Student = c(1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3),
Month = c(1, 2, 3, 5, 6, 7, 8, 11, 12,
2, 3, 4, 5, 7, 8, 9, 10,
1, 2, 3, 5, 6, 7, 8, 9))

DT
Student Month
1: 1 1
2: 1 2
3: 1 3
4: 1 5
5: 1 6
6: 1 7
7: 1 8
8: 1 11
9: 1 12
10: 2 2
11: 2 3
12: 2 4
13: 2 5
14: 2 7
15: 2 8
16: 2 9
17: 2 10
18: 3 1
19: 3 2
20: 3 3
21: 3 5
22: 3 6
23: 3 7
24: 3 8
25: 3 9

我想确定连续三个月的时期(由时期的第一个月确定)。这是数据表和合格期间的可视化。
       1   2   3   4   5   6   7   8   9   10  11  12


1 * * * * * * * * *
[-------] [-------]
[-------]


2 * * * * * * * *
[-------] [-------]
[-------] [-------]


3 * * * * * * * *
[-------] [-------]
[-------]
[-------]

期望的输出:
id   First_month_in_the_period 

1 1
1 5
1 6
2 2
2 3
2 7
2 8
3 1
3 5
3 6
3 7

寻找 data.table(或 dplyr)解决方案。

最佳答案

使用 standard method ( cumsum...diff...condition ) 来识别连续值的运行,然后将其用作与“学生”一起的分组变量。在每个组内,根据每次运行的长度创建序列并添加到第一个月。

DT[ , .(start = if(.N >= 3) Month[1] + 0:(.N - 3)),
by = .(Student, r = cumsum(c(1L, diff(Month) > 1)))]
# Student r start
# 1: 1 1 1
# 2: 1 2 5
# 3: 1 2 6
# 4: 2 3 2
# 5: 2 3 3
# 6: 2 4 7
# 7: 2 4 8
# 8: 3 4 1
# 9: 3 5 5
# 10: 3 5 6
# 11: 3 5 7

等效 dplyr选择:
DT %>% 
group_by(Student, r = cumsum(c(1L, diff(Month) > 1))) %>%
summarise(list(data.frame(start = if(n() >= 3) Month[1] + 0:(n() - 3)))) %>%
tidyr::unnest()

# # A tibble: 11 x 3
# # Groups: Student [3]
# Student r start
# <dbl> <int> <dbl>
# 1 1 1 1
# 2 1 2 5
# 3 1 2 6
# 4 2 3 2
# 5 2 3 3
# 6 2 4 7
# 7 2 4 8
# 8 3 4 1
# 9 3 5 5
# 10 3 5 6
# 11 3 5 7

关于r - 在组中的 data.table 字段中识别由 n 个连续数字组成的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56307795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com