R 将时间戳数据分离到存储箱中
我有一个文本文件,其中包含与按钮按下相关的时间戳。 我使用R studio将其加载到R中。 按钮按下被格式化为字符串R 将时间戳数据分离到存储箱中,r,R,我有一个文本文件,其中包含与按钮按下相关的时间戳。 我使用R studio将其加载到R中。 按钮按下被格式化为字符串 52 right 08:16:23 53 a 08:16:23 54 up 08:16:24 55 a 08:16:24 56 b 08:16:24 57 a 08:16:24 58 a 08:16:24 59 right 08:16:24 60 a 08:16:24 时间戳已转换为POSIXct时间戳,但在
52 right 08:16:23
53 a 08:16:23
54 up 08:16:24
55 a 08:16:24
56 b 08:16:24
57 a 08:16:24
58 a 08:16:24
59 right 08:16:24
60 a 08:16:24
时间戳已转换为POSIXct时间戳,但在我的文本文件中有单独的日期和时间字段
我想根据时间将数据分成等距的存储箱,并计算其中每个按钮的频率
有很多按钮,有很多不同的非唯一时间戳
理想情况下,我希望间隔时间尽可能短,如果有一个解决方案允许我更改粒度,那就太好了。您可能会对这些功能感兴趣: 答案取决于时间是否被R识别。如果不是,您可以使用
chron( ... )
在你的时间变量上。请参阅:
这将返回每个存储箱中的频率您可能感兴趣的这些功能: 答案取决于时间是否被R识别。如果不是,您可以使用
chron( ... )
在你的时间变量上。请参阅:
这将返回每个bin中的频率让我们假设您有一个名为“dat”的data.frame,并且时间值位于名为“V3”的列中,就像我在您的文本中创建的一样。然后使用间隔一分钟的
seq.POSIXct
只创建一个点,而cut无法处理这个问题,因此我开始添加不同的值。在这个过程中,我发现我对seq.POSIXct的初始尝试返回了较高值的NA,因为如果max time中的秒数高于min time,则序列结束,因此我在max中添加了60秒作为此演示的间隔。您应该能够在明显的位置概括代码
# Initial failed attempt with your data
> grp <- cut(dat$time, breaks=seq(min(dat$time), max(dat$time), by="1 min"), include.lowest=TRUE)
Error in cut.default(unclass(x), unclass(breaks), labels = labels, right = right, :
'breaks' are not unique
# Better data, more challenging, allows better testing
dat$grp <- cut(dat$time, breaks=seq(min(dat$time),
max(dat$time)+60, by="1 min"),
include.lowest=TRUE,right=TRUE)
> dat
V1 V2 V3 time grp
1 52 right 08:16:23 2016-04-17 08:16:23 2016-04-17 08:15:24
2 53 a 08:16:23 2016-04-17 08:16:23 2016-04-17 08:15:24
3 54 up 08:17:59 2016-04-17 08:17:59 2016-04-17 08:17:24
4 55 a 08:18:45 2016-04-17 08:18:45 2016-04-17 08:18:24
5 56 b 08:20:53 2016-04-17 08:20:53 2016-04-17 08:20:24
6 57 a 08:20:01 2016-04-17 08:20:01 2016-04-17 08:19:24
7 58 a 08:17:5 2016-04-17 08:17:05 2016-04-17 08:16:24
8 59 right 08:18:24 2016-04-17 08:18:24 2016-04-17 08:17:24
9 60 a 08:14:24 2016-04-17 08:14:24 2016-04-17 08:14:24
有关处理缺失值的其他选项,请参见
?表
。假设您有一个名为“dat”的data.frame,并且时间值位于名为“V3”的列中,因为它位于我创建的表单文本中。然后使用间隔一分钟的seq.POSIXct
只创建一个点,而cut无法处理这个问题,因此我开始添加不同的值。在这个过程中,我发现我对seq.POSIXct的初始尝试返回了较高值的NA,因为如果max time中的秒数高于min time,则序列结束,因此我在max中添加了60秒作为此演示的间隔。您应该能够在明显的位置概括代码
# Initial failed attempt with your data
> grp <- cut(dat$time, breaks=seq(min(dat$time), max(dat$time), by="1 min"), include.lowest=TRUE)
Error in cut.default(unclass(x), unclass(breaks), labels = labels, right = right, :
'breaks' are not unique
# Better data, more challenging, allows better testing
dat$grp <- cut(dat$time, breaks=seq(min(dat$time),
max(dat$time)+60, by="1 min"),
include.lowest=TRUE,right=TRUE)
> dat
V1 V2 V3 time grp
1 52 right 08:16:23 2016-04-17 08:16:23 2016-04-17 08:15:24
2 53 a 08:16:23 2016-04-17 08:16:23 2016-04-17 08:15:24
3 54 up 08:17:59 2016-04-17 08:17:59 2016-04-17 08:17:24
4 55 a 08:18:45 2016-04-17 08:18:45 2016-04-17 08:18:24
5 56 b 08:20:53 2016-04-17 08:20:53 2016-04-17 08:20:24
6 57 a 08:20:01 2016-04-17 08:20:01 2016-04-17 08:19:24
7 58 a 08:17:5 2016-04-17 08:17:05 2016-04-17 08:16:24
8 59 right 08:18:24 2016-04-17 08:18:24 2016-04-17 08:17:24
9 60 a 08:14:24 2016-04-17 08:14:24 2016-04-17 08:14:24
有关处理缺失值的其他选项,请参见
?表
。我认为这是假设我的时间戳是以固定的间隔出现的,但我认为这是假设我的时间戳是以固定的间隔出现的
> table(dat$grp)
2016-04-17 08:14:24 2016-04-17 08:15:24 2016-04-17 08:16:24 2016-04-17 08:17:24
1 2 1 2
2016-04-17 08:18:24 2016-04-17 08:19:24 2016-04-17 08:20:24
1 1 1