根据时间分配变量
我有一个时间序列,并且对添加新变量有特定要求。
这是一些数据
dput(df)
structure(list(Time = structure(c(1567423339.229, 1567423399.018,
1567424218.867, 1567425478.666, 1567425498.883, 1567426519.008,
1567429378.848, 1567429398.979, 1567429978.723, 1567431218.909
), tzone = "", class = c("POSIXct", "POSIXt")), RaceNum = c("1",
"1", "1", "1", "1", "1", "2", "2", "2", "2")), class = "data.frame", row.names = c(NA,
-10L))
我尝试使用 case_when 或 ifelse 做的事情是在 1:nrow
基础上分配 d
,除非下一个时间序列事件在 1 分钟内,然后它采用前一个变量并向其添加 b
。正如您所看到的,当 RaceNum
更改时,编号会重新开始。我通过 RaceNum
拆分 df,然后在建立 d
后将 cbind
重新组合在一起。
这是预期的结果
dput(df2)
structure(list(Time = structure(c(1567423339.229, 1567423399.018,
1567424218.867, 1567425478.666, 1567425498.883, 1567426519.008,
1567429378.848, 1567429398.979, 1567429978.723, 1567431218.909
), tzone = "", class = c("POSIXct", "POSIXt")), RaceNum = c("1",
"1", "1", "1", "1", "1", "2", "2", "2", "2"), d = c("1", "1b",
"2", "3", "3b", "4", "1", "1b", "2", "3")), class = "data.frame", row.names = c(NA,
-10L))
I have a time series and I have a specific requirement for adding a new variable.
Here is some data
dput(df)
structure(list(Time = structure(c(1567423339.229, 1567423399.018,
1567424218.867, 1567425478.666, 1567425498.883, 1567426519.008,
1567429378.848, 1567429398.979, 1567429978.723, 1567431218.909
), tzone = "", class = c("POSIXct", "POSIXt")), RaceNum = c("1",
"1", "1", "1", "1", "1", "2", "2", "2", "2")), class = "data.frame", row.names = c(NA,
-10L))
What I have tried to do unseussesfully using case_when or ifelse is to assign d
on a 1:nrow
basis unless the next time series event is within 1 minute then it takes the previous variable and adds a b
to it. As you can see the numbering starts again whne RaceNum
changes. I was splitting the df by RaceNum
then cbind
back together once I had established d
.
Here is the expected result
dput(df2)
structure(list(Time = structure(c(1567423339.229, 1567423399.018,
1567424218.867, 1567425478.666, 1567425498.883, 1567426519.008,
1567429378.848, 1567429398.979, 1567429978.723, 1567431218.909
), tzone = "", class = c("POSIXct", "POSIXt")), RaceNum = c("1",
"1", "1", "1", "1", "1", "2", "2", "2", "2"), d = c("1", "1b",
"2", "3", "3b", "4", "1", "1b", "2", "3")), class = "data.frame", row.names = c(NA,
-10L))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RaceNum
创建一个变量,当连续记录之间的差异大于 1 分钟时,该变量会递增。d
),将字母
粘贴到组编号中。RaceNum
create a variable which increments when the difference between consecutive records is greater than 1 minute.d
) pasteletters
to group number.