以日历天为单位分割一个间隔的持续时间 [英] Split a duration of an interval in calendar days
问题描述
我有以下数据集显示一集的开始和结束(日期和时间)
I have the following data set which shows the start and the end of an episode (date and time)
ep <- data.frame(start=c("2009-07-13 23:45:00", "2009-08-14 08:30:00",
"2009-09-16 15:30:00"),
end=c("2009-07-14 00:03:00", "2009-08-15 08:35:00",
"2009-09-19 07:30:00"))
我需要将它转换成一个数据框,它会在每个日历日显示有多少分钟的剧集.对于上面的示例,它将是:
I need to convert it into a data frame which would show in each calendar day how many minutes of episodes there were. For the above example it would be:
2009-07-13 15
2009-07-14 3
2009-08-14 930
2009-08-15 515
2009-09-16 510
2009-09-17 1440
2009-09-18 1440
2009-09-19 450
感谢您的帮助
推荐答案
这有效,但似乎有点不雅.首先,创建一个向量,它是每个开始和结束时间之间的时间序列,以分钟为单位:
This works, but seems slightly inelegant. First, create a vector that is a sequence of times between each start and end time by minutes:
tmp <- do.call(c, apply(ep, 1,
function(x) head(seq(from = as.POSIXct(x[1]),
to = as.POSIXct(x[2]),by = "mins"),
-1)))
我们使用 head(...., -1)
从每个序列中删除最后一分钟,以便分钟匹配您想要的.
We use head(...., -1)
to remove the last minute from each sequence so as the minutes match what you wanted.
接下来,将此向量拆分为发生在个别日期的分钟,并计算每天有多少分钟:
Next, split this vector into minutes occurring on individual days, and count how many minuts there are per day:
tmp <- sapply(split(tmp, format(tmp, format = "%Y-%m-%d")), length)
请注意,出于某种原因(可能与时区相关),我们不能仅使用 as.Date(tmp)
来获取日期向量,我们需要将时间显式格式化为仅显示日期部分.
Note that for some reason (probably time-zone related) that we can't just use as.Date(tmp)
to get a vector of dates, we need to explicitly format the times to show only the date parts.
最后一步是将包含我们需要的所有内容的 tmp
对象排列成您要求的格式:
The final step is to arrange the tmp
object that contains everything we need into the format you requested:
mins <- data.frame(Date = names(tmp), Minutes = tmp, row.names = NULL)
这给出:
> mins
Date Minutes
1 2009-07-13 15
2 2009-07-14 3
3 2009-08-14 930
4 2009-08-15 515
5 2009-09-16 510
6 2009-09-17 1440
7 2009-09-18 1440
8 2009-09-19 450
这篇关于以日历天为单位分割一个间隔的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!