计算R的持续时间 [英] Calculate duration in R
问题描述
我想知道是否有一种简单的方法来计算持续时间。我有一个数据集,其中一个名为 m
的参数在值 -1
和 1
。我要计算:
I wonder if there is an easy way to calculate the duration. I have a dataset where the a parameter, called m
, varies between the values -1
and 1
during time. I want to calculate:
-
m = -1 $>的情况下的总持续时间(以小时为单位)
和m = 1
-
每段病例多长时间
m = -1
和m = 1
分别是
- The total duration (time in hours) of cases where
m=-1
andm=1
respectively How long is each period of cases where
m=-1
andm=1
respectively is
m< -c(1,1,1,1,-1,-1,-1,-1,1,1,1,1,1,-1,-1,-1,-1,-1,-1, 1,1,1,1,1,1,1)
m<-c(1,1,1,1,-1,-1,-1,-1,1,1,1,1,1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1)
时间<-seq.POSIXt(as.POSIXct(Sys.Date()),as。 POSIXct(Sys.Date()+ 1),= = 1小时)
Time <- seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date()+1), by = "1 hour")
推荐答案
我将数据包data.table用于 split-apply-combine,并使用 cumsum
和 diff 标识运行。 code>:
I'd use package data.table for "split-apply-combine" and identify the runs using cumsum
and diff
:
DF <- read.table(text="Time, m
2015-01-01 00:00, -1
2015-01-01 01:00, -1
2015-01-01 02:00, -1
2015-01-01 03:00, 1
2015-01-01 04:00, 1
2015-01-01 05:00, 1
2015-01-01 06:00, 1
2015-01-01 07:00, 1
2015-01-01 08:00, -1
2015-01-01 09:00, -1
2015-01-01 10:00, -1
2015-01-01 11:00, -1
2015-01-01 12:00, 1
2015-01-01 13:00, 1
2015-01-01 14:00, 1
2015-01-01 15:00, -1", header = TRUE, sep =",")
library(data.table)
setDT(DF)
DF[, Time := as.POSIXct(Time, format = "%Y-%m-%d %H:%M", tz = "GMT")]
DF[, run := cumsum(c(1, diff(m) != 0))]
DF1 <- DF[, list(m = unique(m),
duration = difftime(max(Time), min(Time), unit = "min")),
by = run]
# run m duration
#1: 1 -1 120 mins
#2: 2 1 240 mins
#3: 3 -1 180 mins
#4: 4 1 120 mins
#5: 5 -1 0 mins
DF1[, sum(duration), by = m]
# m V1
#1: -1 300
#2: 1 360
这篇关于计算R的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!