[英]R datatable grouping based on condition and getting count based on the conditions
I have a data table like this:我有一个这样的数据表:
timestamp type status
05-01-2020 12:07:08 A 1
05-01-2020 12:36:05 A 1
05-01-2020 13:34:25 A 1
05-01-2020 23:45:02 A 1
05-01-2020 23:55:02 B 1
05-01-2020 13:44:33 B 2
06-01-2020 01:07:08 A 1
06-01-2020 10:23:05 A 1
06-01-2020 12:11:08 A 2
06-01-2020 22:06:12 B 2
07-01-2020 00:01:05 A 2
07-01-2020 02:17:09 A 1
07-01-2020 12:36:05 B 1
07-01-2020 12:07:08 B 1
07-01-2020 12:36:05 A 1
07-01-2020 12:36:05 A 1
08-01-2020 12:36:05 B 2
08-01-2020 12:36:05 B 1
08-01-2020 12:36:05 B 1
09-01-2020 12:36:05 B 1
09-01-2020 12:07:08 B 2
09-01-2020 12:36:05 B 1
11-01-2020 12:07:08 A 1
11-01-2020 12:36:05 A 1
I am trying to group it on date and type using rleid()
.我正在尝试使用rleid()
按日期和类型对其进行分组。
dt <- dt[, group_id := rleid(as.IDate(timestamp),type,status = 1)][]
Now I want to get two counts.现在我想得到两个计数。
One is to count the number of instances inside each group which meets the condition per day.一是统计每天每个组内满足条件的实例数。
date type count
05-01-2020 A 4
05-01-2020 B 1
06-01-2020 A 2
07-01-2020 A 3
07-01-2020 B 2
08-01-2020 B 2
09-01-2020 B 2
11-01-2020 A 2
Second one is to find the number of groups per day which meet the condition.第二个是找到每天满足条件的组数。
date type count
05-01-2020 A 1
05-01-2020 B 1
06-01-2020 A 1
07-01-2020 A 2
07-01-2020 B 1
08-01-2020 B 1
09-01-2020 B 2
11-01-2020 A 1
1) To count the number of instances inside each group which meets the condition per day. 1) 统计每个组内每天满足条件的实例数。
library(data.table)
setDT(df)
df[, .(count = sum(status == 1)), .(timestamp, type)]
# timestamp type count
#1: 05-01-2020 A 4
#2: 05-01-2020 B 1
#3: 06-01-2020 A 2
#4: 06-01-2020 B 0
#5: 07-01-2020 A 3
#6: 07-01-2020 B 2
#7: 08-01-2020 B 2
#8: 09-01-2020 B 2
#9: 11-01-2020 A 2
You can remove the 0 counts if they are not needed.如果不需要,您可以删除 0 计数。
2) To find the number of groups per day which meet the condition. 2)查找每天满足条件的组数。
Create a new column ( count_N
) using rleid
of type
and status
and for status = 1
count unique values for each timestamp
and type
.使用type
和status
的rleid
创建一个新列 ( count_N
),对于status = 1
,计算每个timestamp
和type
的唯一值。
df[, count_N := rleid(type, status), timestamp]
df[status == 1, .(count = uniqueN(count_N)), .(timestamp, type)]
# timestamp type count
#1: 05-01-2020 A 1
#2: 05-01-2020 B 1
#3: 06-01-2020 A 1
#4: 07-01-2020 A 2
#5: 07-01-2020 B 1
#6: 08-01-2020 B 1
#7: 09-01-2020 B 2
#8: 11-01-2020 A 1
We can first convert the 'timestamp' to Datetime class with as.POSIXct
and then convert it to Date
class我们可以先使用 as.POSIXct 将“时间戳”转换为日期时间as.POSIXct
,然后将其转换为Date
class
library(data.table)
setDT(dt)[, timestamp := as.POSIXct(timestamp,
format = '%m-%d-%Y %H:%M:%S')][, date := as.IDate(timestamp)]
dt[status == 1, .N, .(date, type)]
#. date type N
#1: 2020-05-01 A 4
#2: 2020-05-01 B 1
#3: 2020-06-01 A 2
#4: 2020-07-01 A 3
#5: 2020-07-01 B 2
#6: 2020-08-01 B 2
#7: 2020-09-01 B 2
#8: 2020-11-01 A 2
For the second case对于第二种情况
dt[, grp := rleid(type, status, date)]
dt[status == 1, .(count = uniqueN(grp)), .(date, type)]
# date type count
#1: 2020-05-01 A 1
#2: 2020-05-01 B 1
#3: 2020-06-01 A 1
#4: 2020-07-01 A 2
#5: 2020-07-01 B 1
#6: 2020-08-01 B 1
#7: 2020-09-01 B 2
#8: 2020-11-01 A 1
dt <- structure(list(timestamp = structure(c(1588349228, 1588350965,
1588354465, 1588391102, 1588391702, 1588355073, 1590988028, 1591021385,
1591027868, 1591063572, 1593576065, 1593584229, 1593621365, 1593619628,
1593621365, 1593621365, 1596299765, 1596299765, 1596299765, 1598978165,
1598976428, 1598978165, 1604250428, 1604252165), class = c("POSIXct",
"POSIXt"), tzone = ""), type = c("A", "A", "A", "A", "B", "B",
"A", "A", "A", "B", "A", "A", "B", "B", "A", "A", "B", "B", "B",
"B", "B", "B", "A", "A"), status = c(1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L,
1L, 1L)), class = "data.frame", row.names = c(NA, -24L),
index = structure(integer(0), "`__status`" = c(1L,
2L, 3L, 4L, 5L, 7L, 8L, 12L, 13L, 14L, 15L, 16L, 18L, 19L, 20L,
22L, 23L, 24L, 6L, 9L, 10L, 11L, 17L, 21L)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.