本文介绍了R:连续计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决此问题,我有一个带有计数数据列表的DF.例如这样的东西:

I am having trouble solving this one, I have a DF with a list of count data. For example something like this:

 DATE        TIME     ID
 2014-02-14  15:02    1
 2014-02-15  15:12    1
 2014-02-16  08:34    2
 2014-02-17  02:02    2
 2014-02-19  11:02    1
 2014-02-20  15:42    1
 2014-02-22  16:02    2
 2014-02-25  15.02    1
 ....

现在,我想创建一个函数来测量连续几天的长度,但是我希望该函数能够设置允许日期分开的天数(默认值= 1天).输出应为DF,每个ID包含多行(因为这些行表示存在多个活动时间段),并且每个连续时间段应显示该时间段所包含的天数.输出示例应为:

Now I want to make a function that measures the length of a consecutive period of days, but I want the function to have the ability to set a number of days that the dates are allowed to be apart (default = 1 day). The output should be a DF which has multiple rows per ID (as these represent that there are multiple periods of activity) and per consecutive periods should display the amount of days that period consisted of. Example of the output should be:

 ID        periodID     days
 1         1            2
 2         1            2
 1         2            2
 ....

第一个问题是,如何构造这样的DF?其次,如何扩展此范围,以便可以在函数中输入确保日期为例如同一时间段中包含相隔2或4天的日期(因此不包含连续的日期,但是日期之间可以相隔> 1天)?

So the first question is, how can I construct a DF like this? Secondly, how can I extend this so that I can have an input in the function that makes sure dates that are e.g. 2 or 4 days apart are included in the same period (so not consecutive dates, but the dates are allowed to be >1 day apart)?

编辑:这是我正在处理的原始数据集的摘要(请注意,同一天有多个数据点,但是时间被忽略,只有日期才重要).

EDIT Here is a snippet of the original dataset I am working on (note that there are multiple data points on the same day, but time is ignored, only the date matters).

    date    time    ID
24-10-14    10:47:36    1
24-10-14    10:52:19    1
24-10-14    10:56:47    1
24-10-14    10:58:57    1
24-10-14    11:39:18    1
24-10-14    12:03:27    1
24-10-14    12:19:22    1
24-10-14    12:43:22    2
24-10-14    12:44:42    2
24-10-14    12:55:47    1
24-10-14    12:58:15    3
24-10-14    14:12:35    1
24-10-14    14:29:20    1
24-10-14    14:42:27    1
24-10-14    14:43:49    1
24-10-14    15:49:46    1
24-10-14    16:03:21    1
24-10-14    16:08:09    1
24-10-14    17:24:34    1
24-10-14    17:25:46    1
24-10-14    17:34:06    1
24-10-14    17:35:26    1
24-10-14    17:45:54    1
24-10-14    17:55:03    1
24-10-14    17:57:37    1
24-10-14    18:01:27    1
24-10-14    18:02:46    1
24-10-14    18:07:02    1
24-10-14    18:08:17    1
24-10-14    18:09:51    1
24-10-14    18:15:04    1
24-10-14    18:16:50    1
24-10-14    18:18:28    1
24-10-14    18:23:14    1
24-10-14    18:33:17    1
24-10-14    18:34:18    1
24-10-14    18:40:06    1
24-10-14    18:41:21    1
25-10-14    03:54:16    4
25-10-14    03:57:18    4
25-10-14    03:58:48    4
25-10-14    04:05:54    4
25-10-14    04:07:30    4
25-10-14    04:08:47    4
25-10-14    04:21:46    4
25-10-14    04:26:59    4
25-10-14    07:33:44    2
25-10-14    07:35:00    2
25-10-14    07:45:23    2
25-10-14    07:56:48    2
25-10-14    08:01:38    2
25-10-14    08:07:05    2
25-10-14    08:08:23    2
25-10-14    08:11:29    2
25-10-14    08:13:23    2
25-10-14    08:15:15    2
25-10-14    08:17:05    2
25-10-14    08:18:50    2
25-10-14    08:24:53    2
25-10-14    08:32:34    2
25-10-14    08:43:50    2
25-10-14    08:50:08    2
25-10-14    08:52:51    2
25-10-14    08:53:55    2
25-10-14    08:56:37    2
25-10-14    08:58:50    2
25-10-14    09:07:31    2
25-10-14    09:08:57    2
25-10-14    09:10:52    2
25-10-14    09:26:54    2
25-10-14    09:28:45    2
25-10-14    09:30:33    2
25-10-14    09:32:11    2
25-10-14    09:35:19    2
25-10-14    09:36:58    2
25-10-14    09:41:46    2
25-10-14    09:54:16    2
25-10-14    10:13:33    2
25-10-14    10:30:30    2
25-10-14    10:32:05    2
25-10-14    10:33:19    2
25-10-14    10:34:49    2
25-10-14    10:35:55    2
25-10-14    10:37:12    2
25-10-14    10:54:39    2
25-10-14    10:57:36    2
25-10-14    11:00:02    2
25-10-14    11:45:42    2
25-10-14    11:49:21    2
25-10-14    11:54:17    2
25-10-14    11:59:10    2
25-10-14    12:02:57    2
25-10-14    12:04:46    2
25-10-14    12:06:28    2
25-10-14    12:10:07    2
25-10-14    12:11:26    2
25-10-14    12:14:39    2
25-10-14    12:16:54    2
25-10-14    12:20:19    2
25-10-14    12:22:37    2
25-10-14    12:24:08    2
25-10-14    12:25:15    2
25-10-14    12:26:33    2
25-10-14    12:28:12    2
25-10-14    12:29:34    2
25-10-14    12:31:25    2
25-10-14    12:33:09    2
25-10-14    12:34:43    2
25-10-14    12:35:53    2
25-10-14    12:37:20    2
25-10-14    12:38:16    2
25-10-14    12:41:16    2
25-10-14    12:42:28    2
25-10-14    12:43:57    2
25-10-14    12:44:57    2
25-10-14    12:46:01    2
25-10-14    12:48:46    2
25-10-14    12:52:50    2
25-10-14    12:54:13    2
25-10-14    12:56:01    2
25-10-14    12:57:45    2
25-10-14    12:59:15    2
25-10-14    13:01:41    2
25-10-14    13:03:21    2
25-10-14    13:04:46    2
25-10-14    13:08:37    2
25-10-14    13:24:01    2
25-10-14    13:25:56    2
25-10-14    13:27:50    2
25-10-14    13:29:42    2
25-10-14    13:34:37    2
25-10-14    13:35:37    2
25-10-14    13:36:40    2
25-10-14    13:37:50    2
25-10-14    13:41:06    2
25-10-14    13:46:02    2
25-10-14    13:51:54    2
25-10-14    13:58:16    2
25-10-14    14:00:07    2
25-10-14    14:01:54    2
25-10-14    14:03:31    2
25-10-14    14:04:50    2
25-10-14    14:13:22    2
25-10-14    14:15:00    2
25-10-14    14:16:21    2
25-10-14    14:18:07    2
25-10-14    14:19:44    2
25-10-14    14:34:44    2
25-10-14    14:41:16    2
25-10-14    14:42:15    2
25-10-14    14:43:17    2
25-10-14    14:44:26    2
25-10-14    15:00:40    2
25-10-14    15:03:35    2
25-10-14    15:08:17    2
25-10-14    15:09:34    2
25-10-14    15:11:11    2
25-10-14    15:12:29    2
25-10-14    15:14:07    2
25-10-14    15:15:30    2
25-10-14    15:30:34    2
25-10-14    15:32:10    2
25-10-14    15:38:01    2
25-10-14    15:41:15    2
25-10-14    15:54:31    2
25-10-14    16:00:12    2
25-10-14    16:01:46    2
25-10-14    16:08:39    2
25-10-14    16:15:28    2
25-10-14    16:18:41    2
25-10-14    16:19:46    2
25-10-14    16:22:37    2
25-10-14    16:29:53    2
25-10-14    16:43:14    2
25-10-14    17:02:41    2
25-10-14    17:06:38    2
25-10-14    17:11:45    2
25-10-14    17:19:21    2
25-10-14    17:22:26    2
25-10-14    17:24:20    2
25-10-14    17:26:13    2
25-10-14    17:28:03    2
25-10-14    17:29:49    2
25-10-14    17:31:24    2
25-10-14    17:34:17    2
25-10-14    17:45:20    2
25-10-14    17:47:10    2
25-10-14    17:48:55    2
25-10-14    17:50:29    2
25-10-14    17:51:40    2
25-10-14    17:53:05    2
25-10-14    17:54:02    2
25-10-14    17:54:59    2
25-10-14    17:55:58    2
25-10-14    17:56:59    2
25-10-14    17:58:07    2
25-10-14    18:03:40    2
25-10-14    18:06:15    2
25-10-14    18:11:48    2
25-10-14    18:13:28    2
25-10-14    18:14:53    2
25-10-14    18:16:45    2
25-10-14    18:20:18    2
25-10-14    18:24:15    2
25-10-14    18:27:21    2
25-10-14    18:29:16    2
25-10-14    18:31:09    2
25-10-14    18:34:47    2
25-10-14    18:36:25    2
25-10-14    18:37:44    2
25-10-14    18:39:28    2
25-10-14    18:42:08    2
25-10-14    18:43:28    2
25-10-14    18:45:12    2
25-10-14    18:46:46    2
25-10-14    18:47:56    2
25-10-14    18:49:21    2
25-10-14    18:51:16    2
25-10-14    18:53:09    2
25-10-14    18:55:00    2
25-10-14    18:56:46    2
25-10-14    18:58:21    2
25-10-14    19:02:26    2
25-10-14    19:06:28    2
25-10-14    19:09:54    2
25-10-14    19:11:15    2
25-10-14    19:13:01    2
25-10-14    19:14:40    2
25-10-14    19:17:48    2
25-10-14    19:19:27    2
25-10-14    19:20:49    2
25-10-14    19:22:35    2
25-10-14    19:24:13    2
25-10-14    19:28:55    2
25-10-14    19:30:12    2
25-10-14    19:33:07    2
25-10-14    19:36:13    2
25-10-14    19:37:10    2
25-10-14    19:39:11    2
25-10-14    19:40:18    2
25-10-14    19:41:38    2
25-10-14    19:43:23    2
25-10-14    19:44:57    2
25-10-14    19:48:41    2
25-10-14    19:54:39    2
25-10-14    19:57:55    2
25-10-14    19:59:43    2
25-10-14    20:01:25    2
25-10-14    20:02:52    2
25-10-14    20:03:52    2
25-10-14    20:07:28    2
25-10-14    20:09:21    2
25-10-14    20:12:56    2
25-10-14    20:14:28    2
25-10-14    20:22:15    2
25-10-14    20:23:31    2
25-10-14    20:26:28    2
25-10-14    20:29:37    2
25-10-14    20:31:42    2
25-10-14    20:34:28    2
25-10-14    20:35:37    2
25-10-14    20:38:43    2
25-10-14    20:43:05    2
25-10-14    20:48:48    2
25-10-14    20:50:03    2
25-10-14    20:59:25    2
25-10-14    21:00:55    2
25-10-14    21:03:13    2
25-10-14    21:04:46    2
25-10-14    21:07:15    2
25-10-14    21:13:29    2
25-10-14    21:25:51    2
25-10-14    21:37:51    2
25-10-14    21:46:37    2
25-10-14    21:54:14    2
25-10-14    22:02:41    2
25-10-14    22:05:49    2
25-10-14    22:06:48    2
25-10-14    22:07:51    2
25-10-14    22:10:26    2
25-10-14    22:12:20    2
25-10-14    22:14:13    2
25-10-14    22:16:04    2
25-10-14    22:21:00    2
25-10-14    22:22:56    2
25-10-14    22:25:05    2
25-10-14    22:26:23    2
25-10-14    22:31:20    2
25-10-14    22:36:27    2
25-10-14    22:39:00    2
25-10-14    22:40:50    2
25-10-14    22:42:35    2
25-10-14    22:45:25    2
25-10-14    22:48:15    2
25-10-14    22:52:29    2
25-10-14    22:53:30    2
25-10-14    22:54:36    2
25-10-14    22:55:52    2
25-10-14    22:57:28    2
25-10-14    22:58:44    2
25-10-14    23:00:22    2
25-10-14    23:03:24    2
25-10-14    23:05:55    2
25-10-14    23:08:29    2
25-10-14    23:12:14    2
25-10-14    23:14:00    2
25-10-14    23:15:38    2
25-10-14    23:16:57    2
25-10-14    23:18:40    2
25-10-14    23:23:58    2
25-10-14    23:25:04    2
25-10-14    23:26:18    2
25-10-14    23:27:52    2
25-10-14    23:29:03    2
25-10-14    23:31:27    2
25-10-14    23:32:24    2
25-10-14    23:35:38    2
25-10-14    23:37:04    2
25-10-14    23:38:00    2
25-10-14    23:40:54    2
25-10-14    23:41:57    2
25-10-14    23:43:06    2
25-10-14    23:46:21    2
25-10-14    23:48:06    2
25-10-14    23:49:42    2
25-10-14    23:50:59    2
25-10-14    23:55:41    2
25-10-14    23:57:13    2
26-10-14    00:03:03    2
26-10-14    00:12:57    2
26-10-14    00:14:37    2
26-10-14    00:16:01    2
26-10-14    00:22:53    2
26-10-14    00:25:04    2
26-10-14    00:26:26    2
26-10-14    00:28:14    2
26-10-14    00:29:55    2
26-10-14    00:31:21    2
26-10-14    00:39:00    2
26-10-14    00:41:22    2
26-10-14    00:46:58    2
26-10-14    00:58:04    2
26-10-14    01:03:39    2
26-10-14    01:05:59    2
26-10-14    01:08:52    2
26-10-14    01:18:00    2
26-10-14    01:19:12    2
26-10-14    01:20:44    2
26-10-14    01:21:52    2
26-10-14    01:26:21    2
26-10-14    01:27:22    2
26-10-14    01:28:29    2
26-10-14    01:29:44    2
26-10-14    01:31:22    2
26-10-14    01:36:45    2
26-10-14    01:38:45    2
26-10-14    01:39:52    2
26-10-14    01:41:13    2
26-10-14    01:42:59    2
26-10-14    01:45:51    2
26-10-14    01:47:28    2
26-10-14    01:50:27    2
26-10-14    01:53:48    2
26-10-14    01:57:34    2
26-10-14    02:01:04    2
26-10-14    02:02:30    2
26-10-14    02:03:29    2
26-10-14    02:04:30    2
26-10-14    02:06:51    2
26-10-14    02:08:27    2
26-10-14    02:09:42    2
26-10-14    02:11:17    2
26-10-14    02:13:59    2
26-10-14    02:15:02    2
26-10-14    02:16:13    2
26-10-14    02:17:38    2
26-10-14    02:21:38    2
26-10-14    02:22:53    2
26-10-14    02:24:26    2
26-10-14    02:28:55    2
26-10-14    02:32:43    2
26-10-14    02:34:33    2
26-10-14    02:36:17    2
26-10-14    02:37:49    2
26-10-14    02:38:59    2
26-10-14    02:40:22    2
26-10-14    02:42:11    2
26-10-14    02:45:23    2
26-10-14    02:46:28    2
26-10-14    02:47:41    2
26-10-14    02:49:11    2
26-10-14    02:51:32    2
26-10-14    02:53:05    2
26-10-14    02:57:05    2
26-10-14    02:58:33    2
26-10-14    02:59:33    2
26-10-14    03:03:12    2
26-10-14    03:05:05    2
26-10-14    03:06:02    2
26-10-14    03:09:09    2
26-10-14    03:12:05    2
26-10-14    03:13:25    2
26-10-14    03:15:13    2
26-10-14    03:18:13    2
26-10-14    03:20:01    2
26-10-14    03:23:04    2
26-10-14    03:26:40    2
26-10-14    03:28:16    2
26-10-14    03:29:32    2
26-10-14    03:31:08    2
26-10-14    03:32:25    2
26-10-14    03:38:27    2
26-10-14    03:39:23    2
26-10-14    03:40:18    2
26-10-14    03:41:16    2
26-10-14    03:43:16    2
26-10-14    03:44:24    2
26-10-14    03:45:46    2
26-10-14    03:49:15    2
26-10-14    03:50:40    2
26-10-14    03:52:35    2
26-10-14    03:54:29    2
26-10-14    03:56:20    2
26-10-14    04:01:07    2
26-10-14    04:02:54    2
26-10-14    04:04:36    2
26-10-14    04:06:03    2
26-10-14    04:07:00    2
26-10-14    04:08:00    2
26-10-14    04:09:04    2
26-10-14    04:12:51    2
26-10-14    04:14:05    2
26-10-14    04:19:44    2
26-10-14    04:22:20    2
26-10-14    04:26:16    2
26-10-14    04:27:39    2
26-10-14    04:32:51    2
26-10-14    04:34:08    2
26-10-14    04:35:45    2
26-10-14    04:37:04    2
26-10-14    04:41:31    2
26-10-14    04:50:36    2
26-10-14    04:52:31    2
26-10-14    04:58:09    2
26-10-14    05:07:18    2
26-10-14    05:08:40    2
26-10-14    05:10:27    2
26-10-14    05:13:26    2
26-10-14    05:15:13    2
26-10-14    05:18:08    2
26-10-14    05:23:09    2
26-10-14    05:30:25    2
26-10-14    05:31:54    2
26-10-14    05:35:34    2
26-10-14    05:41:16    2
26-10-14    05:48:08    2
26-10-14    05:50:15    2
26-10-14    05:53:09    2
26-10-14    06:08:53    2
26-10-14    06:09:54    2
26-10-14    06:28:14    2
26-10-14    06:29:41    2
26-10-14    06:49:16    2
26-10-14    07:02:03    2
26-10-14    07:03:01    2
26-10-14    07:06:17    2
26-10-14    07:13:07    2
26-10-14    07:14:46    2
26-10-14    07:16:08    2
26-10-14    07:17:57    2
26-10-14    07:30:21    2
26-10-14    07:32:09    2
26-10-14    07:40:32    2
26-10-14    07:41:58    2
26-10-14    07:45:48    2
26-10-14    07:53:20    2
26-10-14    08:03:58    2
26-10-14    08:07:38    2
26-10-14    08:11:39    2
26-10-14    08:13:32    2
26-10-14    08:14:31    2
26-10-14    08:19:58    2
26-10-14    08:26:07    2
26-10-14    08:27:19    2
26-10-14    08:28:47    2
26-10-14    08:29:47    2
26-10-14    08:30:51    2
26-10-14    08:32:04    2
26-10-14    08:33:33    2
26-10-14    08:40:21    2
26-10-14    08:43:17    2
26-10-14    08:46:25    2
26-10-14    08:48:03    2
26-10-14    08:49:22    2
26-10-14    08:51:06    2
26-10-14    08:52:36    2
26-10-14    08:53:42    2
26-10-14    08:56:30    2
26-10-14    09:02:38    2
26-10-14    09:07:40    2
26-10-14    09:11:30    2
26-10-14    09:15:16    2
26-10-14    09:17:04    2
26-10-14    09:18:44    2
26-10-14    09:21:06    2
26-10-14    09:22:59    2
26-10-14    09:24:56    2
26-10-14    09:25:59    2
26-10-14    09:34:06    2
26-10-14    09:35:43    2
26-10-14    09:37:03    2
26-10-14    09:55:22    2
26-10-14    10:00:47    2
26-10-14    10:15:33    2
26-10-14    10:16:48    2
26-10-14    10:27:21    2
26-10-14    10:33:14    2
26-10-14    10:34:13    2
26-10-14    10:48:28    2
26-10-14    10:49:33    2
26-10-14    10:52:18    2
26-10-14    10:53:26    2
26-10-14    11:07:09    2
26-10-14    11:26:58    2
26-10-14    11:42:23    2
26-10-14    11:44:03    2
26-10-14    11:45:29    2
26-10-14    11:47:24    2
26-10-14    11:51:10    2
26-10-14    11:53:00    2
26-10-14    12:03:45    2
26-10-14    12:07:09    2
26-10-14    12:10:07    2
26-10-14    12:11:51    2
26-10-14    12:13:24    2
26-10-14    12:17:48    2
26-10-14    12:19:35    2
26-10-14    12:21:14    2
26-10-14    12:22:34    2
26-10-14    12:26:01    2
26-10-14    12:29:13    2
26-10-14    12:32:30    2
26-10-14    12:33:39    2
26-10-14    12:35:01    2
26-10-14    12:36:52    2
26-10-14    12:40:15    2
26-10-14    12:41:31    2
26-10-14    12:43:09    2
26-10-14    12:44:28    2
26-10-14    12:46:12    2
26-10-14    13:07:30    2
26-10-14    13:09:24    2
26-10-14    13:11:17    2
26-10-14    13:13:06    2
26-10-14    13:17:29    2
26-10-14    13:20:33    2
26-10-14    13:28:01    2
26-10-14    13:36:00    2
26-10-14    13:40:11    2
26-10-14    13:42:34    2
26-10-14    13:49:32    2
26-10-14    14:00:04    2
26-10-14    14:12:12    2
26-10-14    14:15:58    2
26-10-14    14:20:50    2
26-10-14    14:28:10    2
26-10-14    14:34:04    2
26-10-14    14:45:09    2
26-10-14    14:48:27    2
26-10-14    14:49:23    2
26-10-14    14:52:16    2
26-10-14    15:00:36    2
26-10-14    15:04:02    2
26-10-14    15:14:01    2
26-10-14    15:17:32    2
26-10-14    15:19:00    2
26-10-14    15:24:27    2
26-10-14    15:27:51    2
26-10-14    15:39:12    2
26-10-14    15:42:13    2
26-10-14    15:47:10    2
26-10-14    15:50:23    2
26-10-14    15:57:00    2
26-10-14    16:01:56    2
26-10-14    16:03:13    2
26-10-14    16:04:53    2
26-10-14    16:09:54    2
26-10-14    16:11:33    2
26-10-14    16:12:55    2
26-10-14    16:17:49    2
26-10-14    16:23:35    2
26-10-14    16:25:31    2
26-10-14    16:27:26    2
26-10-14    16:33:02    2
26-10-14    16:34:47    2
26-10-14    16:37:34    2
26-10-14    16:39:04    2
26-10-14    16:40:07    2
26-10-14    16:42:40    2
26-10-14    16:47:59    2
26-10-14    16:49:26    2
26-10-14    16:50:23    2
26-10-14    16:53:34    2
26-10-14    16:54:58    2
26-10-14    16:56:51    2
26-10-14    16:58:40    2
26-10-14    17:00:23    2
26-10-14    17:01:52    2
26-10-14    17:04:06    2
26-10-14    17:05:32    2
26-10-14    17:10:35    2
26-10-14    17:11:56    2
26-10-14    17:13:44    2
26-10-14    17:15:23    2
26-10-14    17:16:47    2
26-10-14    17:18:37    2
26-10-14    17:20:22    2
26-10-14    17:21:56    2
26-10-14    17:24:36    2
26-10-14    17:25:38    2
26-10-14    17:26:44    2
26-10-14    17:31:00    2
26-10-14    17:35:49    2
26-10-14    17:37:38    2
26-10-14    17:39:18    2
26-10-14    17:40:46    2
26-10-14    17:41:45    2
26-10-14    17:42:47    2
26-10-14    17:43:53    2
26-10-14    17:45:13    2
26-10-14    17:46:56    2
26-10-14    17:49:32    2
26-10-14    17:50:47    2
26-10-14    17:52:23    2
26-10-14    17:55:15    2
26-10-14    17:56:33    2
26-10-14    17:58:13    2
26-10-14    18:01:30    2
26-10-14    18:03:19    2
26-10-14    18:05:02    2
26-10-14    18:09:00    2
26-10-14    18:10:42    2
26-10-14    18:12:10    2
26-10-14    18:18:51    2
26-10-14    18:19:54    2
26-10-14    18:21:07    2
26-10-14    18:22:36    2
26-10-14    18:29:38    2
26-10-14    18:31:05    2
26-10-14    18:33:02    2
26-10-14    18:36:44    2
26-10-14    18:37:43    2
26-10-14    18:39:55    2
26-10-14    18:41:18    2
26-10-14    18:43:10    2
26-10-14    18:44:56    2
26-10-14    18:46:33    2
26-10-14    18:51:03    2
26-10-14    18:59:34    2
26-10-14    19:00:33    2
26-10-14    19:01:39    2
26-10-14    19:02:53    2
26-10-14    19:04:27    2
26-10-14    19:05:37    2
26-10-14    19:07:04    2
26-10-14    19:08:03    2
26-10-14    19:09:05    2
26-10-14    19:11:32    2
26-10-14    19:13:16    2
26-10-14    19:14:47    2
26-10-14    19:15:53    2
26-10-14    19:17:09    2
26-10-14    19:18:45    2
26-10-14    19:20:02    2
26-10-14    19:23:01    2
26-10-14    19:29:32    2
26-10-14    19:32:24    2
26-10-14    19:43:03    2
26-10-14    19:46:33    2
26-10-14    19:47:59    2
26-10-14    19:48:55    2
26-10-14    19:50:55    2
26-10-14    19:54:57    2
26-10-14    19:59:40    2
26-10-14    20:02:33    2
26-10-14    20:08:41    2
26-10-14    20:13:05    2
26-10-14    20:14:25    2
26-10-14    20:16:10    2
26-10-14    20:17:44    2
26-10-14    20:18:55    2
26-10-14    20:20:21    2
26-10-14    20:21:16    2
26-10-14    20:23:08    2
26-10-14    20:24:04    2
26-10-14    20:25:01    2
26-10-14    20:25:58    2
26-10-14    20:36:01    2
26-10-14    20:43:17    2
26-10-14    20:45:45    2
26-10-14    20:50:26    2
26-10-14    20:55:18    2
26-10-14    21:01:17    2
26-10-14    21:03:30    2
26-10-14    21:04:55    2
26-10-14    21:12:07    2
26-10-14    21:19:23    2
26-10-14    21:30:13    2
26-10-14    21:32:42    2
26-10-14    21:37:49    2
26-10-14    21:40:29    2
26-10-14    21:46:16    2
26-10-14    21:47:40    2
26-10-14    21:51:25    2
26-10-14    21:54:50    2
26-10-14    22:00:58    2
26-10-14    22:02:46    2
26-10-14    22:04:25    2
26-10-14    22:05:50    2
26-10-14    22:07:45    2
26-10-14    22:11:29    2
26-10-14    22:14:52    2
26-10-14    22:19:02    2
26-10-14    22:20:43    2
26-10-14    22:23:04    2
26-10-14    22:25:00    2
26-10-14    22:26:02    2
26-10-14    22:27:10    2
26-10-14    22:30:17    2
26-10-14    22:31:52    2
26-10-14    22:35:50    2
26-10-14    22:38:13    2
26-10-14    22:40:05    2
26-10-14    22:41:05    2
26-10-14    22:43:18    2
26-10-14    22:46:35    2
26-10-14    22:48:25    2
26-10-14    22:50:11    2
26-10-14    22:51:45    2
26-10-14    22:52:58    2
26-10-14    22:54:30    2
26-10-14    22:55:38    2
26-10-14    22:56:58    2
26-10-14    23:04:04    2
26-10-14    23:05:15    2
26-10-14    23:06:43    2
26-10-14    23:14:33    2
26-10-14    23:25:14    2
26-10-14    23:32:05    2
26-10-14    23:35:16    2
26-10-14    23:46:54    2
26-10-14    23:54:22    2
26-10-14    23:55:30    2
26-10-14    23:59:56    2
27-10-14    00:00:52    2
27-10-14    00:01:51    2
27-10-14    00:02:52    2
27-10-14    00:05:15    2
27-10-14    00:11:17    2
27-10-14    00:17:16    2
27-10-14    00:18:34    2
27-10-14    00:21:37    2
27-10-14    00:33:09    2
27-10-14    00:35:03    2
27-10-14    00:38:46    2
27-10-14    00:54:25    2
27-10-14    00:57:14    2
27-10-14    01:00:53    2
27-10-14    01:03:10    2
27-10-14    01:15:01    2
27-10-14    01:16:03    2
27-10-14    01:17:12    2
27-10-14    01:18:33    2
27-10-14    01:20:21    2
27-10-14    01:22:02    2
27-10-14    01:23:27    2
27-10-14    01:25:21    2
27-10-14    01:27:14    2
27-10-14    01:29:02    2
27-10-14    01:30:45    2
27-10-14    01:32:14    2
27-10-14    01:33:19    2
27-10-14    01:34:33    2
27-10-14    01:36:04    2
27-10-14    01:37:11    2
27-10-14    01:38:28    2
27-10-14    01:40:08    2
27-10-14    01:41:30    2
27-10-14    01:43:19    2
27-10-14    01:45:01    2
27-10-14    01:46:29    2
27-10-14    01:47:28    2
27-10-14    01:51:08    2
27-10-14    01:52:04    2
27-10-14    01:53:00    2
27-10-14    01:53:56    2
27-10-14    01:54:51    2
27-10-14    01:55:47    2
27-10-14    01:56:42    2
27-10-14    01:57:40    2
27-10-14    01:58:39    2
27-10-14    01:59:41    2
27-10-14    02:00:49    2
27-10-14    02:02:09    2
27-10-14    02:05:31    2
27-10-14    02:06:47    2
27-10-14    02:08:23    2
27-10-14    02:09:38    2
27-10-14    02:11:12    2
27-10-14    02:15:15    2
27-10-14    02:16:50    2
27-10-14    02:26:58    2
27-10-14    02:28:51    2
27-10-14    02:30:42    2
27-10-14    02:32:28    2
27-10-14    02:34:04    2
27-10-14    02:36:59    2
27-10-14    02:38:19    2
27-10-14    02:42:40    2
27-10-14    02:43:58    2
27-10-14    02:47:06    2
27-10-14    02:48:04    2
27-10-14    02:50:04    2
27-10-14    02:51:11    2
27-10-14    02:52:30    2
27-10-14    02:55:39    2
27-10-14    02:57:47    2
27-10-14    03:02:09    2
27-10-14    03:07:51    2
27-10-14    03:20:06    2
27-10-14    03:21:46    2
27-10-14    03:25:04    2
27-10-14    03:26:53    2
27-10-14    03:28:37    2
27-10-14    03:31:20    2
27-10-14    03:34:39    2
27-10-14    03:36:30    2
27-10-14    03:38:17    2
27-10-14    03:39:55    2
27-10-14    03:41:14    2
27-10-14    03:45:31    2
27-10-14    03:46:43    2
27-10-14    03:48:12    2
27-10-14    03:49:15    2
27-10-14    03:53:29    2
27-10-14    03:55:07    2
27-10-14    03:56:26    2
27-10-14    04:01:40    2
27-10-14    04:02:53    2
27-10-14    04:04:25    2
27-10-14    04:05:31    2
27-10-14    04:06:48    2
27-10-14    04:08:26    2
27-10-14    04:09:47    2
27-10-14    04:11:33    2
27-10-14    04:13:10    2
27-10-14    04:16:10    2
27-10-14    04:17:36    2
27-10-14    04:18:33    2
27-10-14    04:26:26    2
27-10-14    04:32:49    2
27-10-14    04:33:53    2
27-10-14    04:35:04    2
27-10-14    04:39:29    2
27-10-14    04:40:40    2
27-10-14    04:42:05    2
27-10-14    04:44:00    2
27-10-14    04:45:55    2
27-10-14    04:47:51    2
27-10-14    04:49:46    2
27-10-14    04:51:40    2
27-10-14    04:55:23    2
27-10-14    04:57:07    2
27-10-14    04:59:49    2
27-10-14    05:01:14    2
27-10-14    05:08:31    2
27-10-14    05:09:59    2
27-10-14    05:10:57    2
27-10-14    05:13:06    2
27-10-14    05:14:25    2
27-10-14    05:18:42    2
27-10-14    05:23:39    2
27-10-14    05:25:05    2
27-10-14    05:26:03    2
27-10-14    05:27:01    2
27-10-14    05:28:03    2
27-10-14    05:29:13    2
27-10-14    05:30:37    2
27-10-14    05:37:12    2
27-10-14    05:38:55    2
27-10-14    05:45:42    2
27-10-14    05:47:19    2
27-10-14    05:48:39    2
27-10-14    05:50:21    2
27-10-14    05:58:07    2
27-10-14    06:05:00    2
27-10-14    06:10:55    2
27-10-14    06:17:05    2
27-10-14    06:27:54    2
27-10-14    06:29:01    2
27-10-14    06:33:39    2
27-10-14    06:38:04    2
27-10-14    06:46:34    2
27-10-14    06:48:20    2
27-10-14    06:49:55    2
27-10-14    06:53:55    2
27-10-14    06:57:15    2
27-10-14    06:59:18    2
27-10-14    07:00:28    2
27-10-14    07:01:51    2
27-10-14    07:03:44    2
27-10-14    07:05:34    2
27-10-14    07:07:17    2
27-10-14    07:08:48    2
27-10-14    07:09:55    2
27-10-14    07:11:11    2
27-10-14    07:14:14    2
27-10-14    07:20:31    2
27-10-14    07:21:57    2
27-10-14    07:22:52    2
27-10-14    07:24:43    2
27-10-14    07:25:38    2
27-10-14    07:26:33    2
27-10-14    07:27:28    2
27-10-14    07:28:24    2
27-10-14    07:30:21    2
27-10-14    07:31:24    2
27-10-14    07:32:34    2
27-10-14    07:34:00    2
27-10-14    07:35:54    2
27-10-14    07:37:49    2
27-10-14    07:41:41    2
27-10-14    07:49:21    2
27-10-14    07:51:15    2
27-10-14    07:53:08    2
27-10-14    07:54:57    2
27-10-14    08:00:35    2
27-10-14    08:02:12    2
27-10-14    08:08:05    2
27-10-14    08:09:36    2
27-10-14    08:10:42    2
27-10-14    08:11:58    2
27-10-14    08:13:33    2
27-10-14    08:14:49    2
27-10-14    08:16:25    2
27-10-14    08:17:41    2
27-10-14    08:19:17    2
27-10-14    08:20:33    2
27-10-14    08:22:12    2
27-10-14    08:23:32    2
27-10-14    08:25:18    2
27-10-14    08:26:54    2
27-10-14    08:28:11    2
27-10-14    08:29:48    2
27-10-14    08:31:06    2
27-10-14    08:32:46    2
27-10-14    08:34:12    2
27-10-14    08:37:59    2
27-10-14    08:39:51    2
27-10-14    08:44:35    2
27-10-14    08:46:22    2
27-10-14    08:52:19    2
27-10-14    08:54:09    2
27-10-14    08:55:54    2
27-10-14    08:57:26    2
27-10-14    08:58:38    2
27-10-14    09:00:04    2
27-10-14    09:01:02    2
27-10-14    09:02:00    2
27-10-14    09:03:03    2
27-10-14    09:04:13    2
27-10-14    09:05:37    2
27-10-14    09:09:17    2
27-10-14    09:10:57    2
27-10-14    09:12:21    2
27-10-14    09:14:13    2
27-10-14    09:16:02    2
27-10-14    09:17:44    2
27-10-14    09:19:13    2
27-10-14    09:20:14    2
27-10-14    09:21:21    2
27-10-14    09:22:39    2
27-10-14    09:24:17    2
27-10-14    09:25:40    2
27-10-14    09:27:29    2
27-10-14    09:29:13    2
27-10-14    09:30:45    2
27-10-14    09:33:18    2
27-10-14    09:36:53    2
27-10-14    09:38:27    2
27-10-14    09:39:39    2
27-10-14    09:41:06    2
27-10-14    09:42:06    2
27-10-14    09:43:09    2
27-10-14    09:46:51    2
27-10-14    09:47:57    2
27-10-14    09:49:13    2
27-10-14    09:50:49    2
27-10-14    09:52:06    2
27-10-14    09:55:04    2

推荐答案

我们可以使用 dplyr .首先,将您的数据转换为正确的日期格式:

We can use dplyr. First, convert your data in proper date format:

z$date <- as.Date(as.character(z$date), format = "%d-%m-%y")

现在功能:

library(dplyr)
runchecker <- function(data, days){
  data %>% arrange(date) %>%
           group_by(ID) %>%
           mutate(diff = c(0, diff(date)),
                  periodID = 1 + cumsum(diff > days)) %>%
           group_by(ID, periodID) %>%
           summarise(days = last(date) - first(date))
}

并在您的数据上调用它:

and call it on your data:

runchecker(data, 1)
Source: local data frame [4 x 3]
Groups: ID [?]

     ID periodID   days
  (int)    (dbl) (dfft)
1     1        1 0 days
2     2        1 3 days
3     3        1 0 days
4     4        1 0 days

请注意,您的数据几乎没有日期更改或间隔,也许包括更好的测试数据集?

NB your data has very few date changes or gaps, maybe include a better data set for testing?

这篇关于R:连续计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:55