如何计算同一变量期间的时间长度

时间:2014-08-18 08:37:25

标签: r plot dataframe

我的问题有点复杂。这是我数据的一小部分。状态栏表示车辆感应回路的状态。 " 0"表示空置," 1"被占领了。 "颜色"显示红绿灯状态。

我的目标是分析车辆在探测器中停留的时间与一天中的时间,光的颜色之间的关系。这对我来说是个大问题。

所以目前我的问题是,如何计算每次成功的时间长度" 1"序列?然后将其绘制为y轴,x轴是变为" 1" Thx的开始时间以获得任何帮助。

     time                   state color
20394 2014-08-04 08:01:52.0    0   red
20395 2014-08-04 08:01:52.5    0   red
20396 2014-08-04 08:01:53.0    1   red
20397 2014-08-04 08:01:53.2    1   red
20398 2014-08-04 08:01:54.0    1   red
20399 2014-08-04 08:01:54.2    1   red
20400 2014-08-04 08:01:55.0    1   red
20401 2014-08-04 08:01:55.7    1   red
20402 2014-08-04 08:01:56.0    1   red
20403 2014-08-04 08:01:56.0    1   red
20404 2014-08-04 08:01:56.2    1   red
20405 2014-08-04 08:01:57.0    1 green
20406 2014-08-04 08:01:57.2    1 green
20407 2014-08-04 08:02:01.0    1 green
20408 2014-08-04 08:02:01.0    0 green
20409 2014-08-04 08:02:01.2    0 green
20410 2014-08-04 08:02:01.5    0 green
20411 2014-08-04 08:02:02.0    0 green
20412 2014-08-04 08:02:02.2    0 green
20413 2014-08-04 08:02:03.2    0 green
20414 2014-08-04 08:02:03.4    0 green
20415 2014-08-04 08:02:05.2    0 green
20416 2014-08-04 08:02:05.7    0 green
20417 2014-08-04 08:02:06.0    0 green
20418 2014-08-04 08:02:07.0    0 green
20419 2014-08-04 08:02:07.7    0 green
20420 2014-08-04 08:02:08.0    0 green
20421 2014-08-04 08:02:08.7    0 green
20422 2014-08-04 08:02:09.0    0 green
20423 2014-08-04 08:02:09.4    0 green
20424 2014-08-04 08:02:10.4    0 green
20425 2014-08-04 08:02:10.7    0 green
20426 2014-08-04 08:02:11.0    0 green
20427 2014-08-04 08:02:11.2    0 green
20428 2014-08-04 08:02:13.0    0 green
20429 2014-08-04 08:02:13.2    0 green
20430 2014-08-04 08:02:13.9    0 green
20431 2014-08-04 08:02:14.4    0 green
20432 2014-08-04 08:02:14.7    0 green
20433 2014-08-04 08:02:15.0    0 green
20434 2014-08-04 08:02:15.2    0 green
20435 2014-08-04 08:02:15.4    0 green
20436 2014-08-04 08:02:15.7    0 green
20437 2014-08-04 08:02:16.0    0 green
20438 2014-08-04 08:02:16.0    0 green
20439 2014-08-04 08:02:17.0    0 green
20440 2014-08-04 08:02:18.2    0 green
20441 2014-08-04 08:02:18.5    0 green
20442 2014-08-04 08:02:18.7    0 green
20443 2014-08-04 08:02:19.4    0 green
20444 2014-08-04 08:02:20.7    0 green
20445 2014-08-04 08:02:21.0    0 green
20446 2014-08-04 08:02:25.0    0 amber
20447 2014-08-04 08:02:25.0    0 amber
20448 2014-08-04 08:02:25.2    0 amber
20449 2014-08-04 08:02:25.9    0 green
20450 2014-08-04 08:02:26.0    0 amber
20451 2014-08-04 08:02:27.0    0 amber
20452 2014-08-04 08:02:28.0    0   red
20453 2014-08-04 08:02:30.0    0   red
20454 2014-08-04 08:02:30.0    0   red
20455 2014-08-04 08:02:30.2    0   red
20456 2014-08-04 08:02:31.0    0   red
20457 2014-08-04 08:02:31.0    0   red
20458 2014-08-04 08:02:31.2    0   red
20459 2014-08-04 08:02:32.0    0   red
20460 2014-08-04 08:02:32.0    0   red
20461 2014-08-04 08:02:32.2    0   red
20462 2014-08-04 08:02:33.0    0   red
20463 2014-08-04 08:02:34.0    0   red
20464 2014-08-04 08:02:34.2    0   red
20465 2014-08-04 08:02:34.9    0   red
20466 2014-08-04 08:02:35.7    0   red
20467 2014-08-04 08:02:36.0    0   red
20468 2014-08-04 08:02:37.0    0   red
20469 2014-08-04 08:02:37.5    0   red
20470 2014-08-04 08:02:37.5    0   red
20471 2014-08-04 08:02:37.7    0   red
20472 2014-08-04 08:02:38.0    0   red
20473 2014-08-04 08:02:38.5    0   red
20474 2014-08-04 08:02:39.0    0   red
20475 2014-08-04 08:02:39.5    0   red
20476 2014-08-04 08:02:40.0    0   red
20477 2014-08-04 08:02:40.7    0   red
20478 2014-08-04 08:02:40.9    0   red
20479 2014-08-04 08:02:41.0    0   red
20480 2014-08-04 08:02:41.2    0   red
20481 2014-08-04 08:02:41.4    0   red
20482 2014-08-04 08:02:42.2    0   red
20483 2014-08-04 08:02:42.7    0   red
20484 2014-08-04 08:02:44.0    0   red
20485 2014-08-04 08:02:44.7    0   red
20486 2014-08-04 08:02:44.9    0   red
20487 2014-08-04 08:02:46.0    0   red
20488 2014-08-04 08:02:47.0    0   red
20489 2014-08-04 08:02:48.0    0   red
20490 2014-08-04 08:02:49.5    0   red
20491 2014-08-04 08:02:49.7    0   red
20492 2014-08-04 08:02:50.4    0   red
20493 2014-08-04 08:02:51.0    0   red
20494 2014-08-04 08:02:52.0    0   red
20495 2014-08-04 08:02:52.7    0   red
20496 2014-08-04 08:02:53.7    0   red
20497 2014-08-04 08:02:55.4    0   red
20498 2014-08-04 08:02:55.5    0   red
20499 2014-08-04 08:02:56.0    0   red
20500 2014-08-04 08:02:57.0    0   red
20501 2014-08-04 08:02:57.2    0   red
20502 2014-08-04 08:02:57.7    0   red
20503 2014-08-04 08:02:57.7    0   red
20504 2014-08-04 08:02:58.7    0   red
20505 2014-08-04 08:03:00.4    0   red
20506 2014-08-04 08:03:00.7    0   red
20507 2014-08-04 08:03:01.0    0   red
20508 2014-08-04 08:03:02.0    0   red
20509 2014-08-04 08:03:06.0    0   red
20510 2014-08-04 08:03:07.2    0   red
20511 2014-08-04 08:03:07.7    0   red
20512 2014-08-04 08:03:08.4    0   red
20513 2014-08-04 08:03:08.5    0   red
20514 2014-08-04 08:03:08.7    1   red
20515 2014-08-04 08:03:09.4    1   red
20516 2014-08-04 08:03:09.9    1   red
20517 2014-08-04 08:03:10.0    1   red
20518 2014-08-04 08:03:10.9    1   red
20519 2014-08-04 08:03:11.0    1   red
20520 2014-08-04 08:03:11.4    1   red
20521 2014-08-04 08:03:11.5    1   red
20522 2014-08-04 08:03:11.5    1   red
20523 2014-08-04 08:03:12.0    1   red
20524 2014-08-04 08:03:12.2    1   red
20525 2014-08-04 08:03:12.5    1   red
20526 2014-08-04 08:03:13.2    1   red
20527 2014-08-04 08:03:13.2    1   red
20528 2014-08-04 08:03:13.4    1   red
20529 2014-08-04 08:03:14.2    1   red
20530 2014-08-04 08:03:14.4    1   red
20531 2014-08-04 08:03:14.9    1   red
20532 2014-08-04 08:03:15.2    1   red
20533 2014-08-04 08:03:16.0    1   red
20534 2014-08-04 08:03:16.5    1   red
20535 2014-08-04 08:03:19.2    1   red
20536 2014-08-04 08:03:19.4    1   red
20537 2014-08-04 08:03:19.5    1   red
20538 2014-08-04 08:03:19.7    1   red
20539 2014-08-04 08:03:22.0    1   red
20540 2014-08-04 08:03:22.2    1   red
20541 2014-08-04 08:03:22.7    1   red
20542 2014-08-04 08:03:23.2    1   red
20543 2014-08-04 08:03:23.4    1   red
20544 2014-08-04 08:03:23.7    1   red
20545 2014-08-04 08:03:24.2    1   red
20546 2014-08-04 08:03:25.2    1   red
20547 2014-08-04 08:03:26.2    1   red
20548 2014-08-04 08:03:26.2    1   red
20549 2014-08-04 08:03:26.4    1   red
20550 2014-08-04 08:03:26.5    1   red
20551 2014-08-04 08:03:27.0    1   red
20552 2014-08-04 08:03:27.2    1 green
20553 2014-08-04 08:03:27.5    1 green
20554 2014-08-04 08:03:28.0    1 green
20555 2014-08-04 08:03:28.2    0 green
20556 2014-08-04 08:03:28.5    0 green
20557 2014-08-04 08:03:30.0    0 green
20558 2014-08-04 08:03:30.5    0 green
20559 2014-08-04 08:03:30.5    0 green
20560 2014-08-04 08:03:31.5    0 green
20561 2014-08-04 08:03:33.0    0 green
20562 2014-08-04 08:03:33.4    0 green
20563 2014-08-04 08:03:33.5    1 green
20564 2014-08-04 08:03:33.9    1 green
20565 2014-08-04 08:03:34.5    1 green
20566 2014-08-04 08:03:34.9    1 green
20567 2014-08-04 08:03:35.4    0 green
20568 2014-08-04 08:03:36.5    0 green
20569 2014-08-04 08:03:37.4    0 green
20570 2014-08-04 08:03:37.5    0 green
20571 2014-08-04 08:03:39.0    0 green
20572 2014-08-04 08:03:42.0    0 green
20573 2014-08-04 08:03:45.0    0 green
20574 2014-08-04 08:03:45.0    0 green
20575 2014-08-04 08:03:48.0    0 green
20576 2014-08-04 08:03:48.2    0 green

1 个答案:

答案 0 :(得分:2)

不确定您的预期结果, 如果dat是数据集:

  indx <- cumsum(c(1,abs(diff(dat$state))))
  indx1 <- !!dat$state
   sapply(split(dat$time[indx1], indx[indx1]), function(x) {
                    time1 <- as.POSIXct(x, format="%Y-%m-%d %H:%M:%S")
                    c(max(time1)-min(time1))})
              2  4  6  
             #8 20  1 #time in seconds

更新

   do.call(rbind,lapply(split(dat$time[indx1], indx[indx1]), function(x) { 
                   time1 <- as.POSIXct(x, format="%Y-%m-%d %H:%M:%S")
                  data.frame(time=x[1], length=c(max(time1)-min(time1))) 
                 }))
      #                time length
   #2 2014-08-04 08:01:53.0      8
   #4 2014-08-04 08:03:08.7     20
   #6 2014-08-04 08:03:33.5      1