选择特定时间范围内的行 [英] Select rows within a particular time range
问题描述
我有一个数据框,例如:
I have a data frame like:
TimeStamp Category
2013-11-02 07:57:18 AM 0
2013-11-02 08:07:19 AM 0
2013-11-02 08:07:21 AM 0
2013-11-02 08:07:25 AM 1
2013-11-02 08:07:29 AM 0
2013-11-02 08:08:18 AM 0
2013-11-02 08:09:20 AM 0
2013-11-02 09:04:18 AM 0
2013-11-02 09:05:22 AM 0
2013-11-02 09:07:18 AM 0
我想做的是在类别
为 1。
在这种情况下,因为 category = 1
在 2013-11-02 08:07:25 AM
,我要选择 07:57:25 AM到08:17内的所有行:25:AM
。
For this case, because category = 1
is at 2013-11-02 08:07:25 AM
, I want to select all rows within 07:57:25 AM to 08:17:25 AM
.
处理此任务的最佳方法是什么?
What is the best way to handle this task?
加法,每个时间范围可能会有多个 1。 (实际数据帧更加复杂,它包含多个具有不同用户的TimeStamp,即还有一个名为 UserID的列)
addition, there maybe multiple "1" for each time frame. (the real data frame is more complicate, it contains multiple TimeStamp with different users, i.e. there is another column named "UserID")
推荐答案
在基R中,无需进行润滑或其他任何操作(假设您要将TimeStamp转换为 POSIXct
对象),例如:
In base R, without lubridate-ing or anything else (assuming that you're going to convert TimeStamp to a POSIXct
object), like:
df$TimeStamp <- as.POSIXct(TimeStamp, format = "%Y-%m-%d %I:%M:%S %p")
df[with(df, abs(difftime(TimeStamp[Category==1],TimeStamp,units="mins")) <= 10 ),]
# TimeStamp Category
#2 2013-11-02 08:07:19 0
#3 2013-11-02 08:07:21 0
#4 2013-11-02 08:07:25 1
#5 2013-11-02 08:07:29 0
#6 2013-11-02 08:08:18 0
#7 2013-11-02 08:09:20 0
如果您有多个 1
's,您必须像这样循环遍历它:
If you've got multiple 1
's, you'd have to loop over it like:
check <- with(df,
lapply(TimeStamp[Category==1], function(x) abs(difftime(x,TimeStamp,units="mins")) <= 10 )
)
df[do.call(pmax, check)==1,]
这篇关于选择特定时间范围内的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!