在时间段内按时间戳加入
我有两个桌子,我想在日期加入。如果一个实例属于颜色期,我希望该实例将上述颜色作为变量接收(请参阅加入):
instance <- read.table(header=TRUE, text="
ID UTC_DateTime Value
1 2022-06-15 15:00:00 8
2 2022-06-15 15:05:00 3
3 2022-06-15 15:10:00 2
4 2022-06-15 15:15:00 4
")
periods <- read.table(header=TRUE, text="
COLOR UTC_DateTime
Start_Green 2022-06-15 14:56:22
End_Green 2022-06-15 15:03:08
Start_Red 2022-06-15 15:03:11
End_Red 2022-06-15 15:58:48
")
joined <- read.table(header=TRUE, text="
ID UTC_DateTime Value Color
1 2022-06-15 15:00:00 8 Green
2 2022-06-15 15:05:00 3 Green
3 2022-06-15 15:10:00 2 Red
4 2022-06-15 15:15:00 4 Red
")
另外,如果您可以以某种方式合并一个将ercepers
to to stripss_wide的过程。
很棒,请参阅:
periods_wide <- read.table(header=TRUE, text="
COLOR UTC_DateTime_Start UTC_DateTime_End
Green 2022-06-15 14:56:22 2022-06-15 15:03:08
Red 2022-06-15 15:03:11 2022-06-15 15:58:48
")
请注意,可能还有更多实例,还有更多的时期(相同或其他颜色)
I have two tables, which I would like to join by date. If an instance falls within a color period, I would like that instance to receive said color as a variable (see joined):
instance <- read.table(header=TRUE, text="
ID UTC_DateTime Value
1 2022-06-15 15:00:00 8
2 2022-06-15 15:05:00 3
3 2022-06-15 15:10:00 2
4 2022-06-15 15:15:00 4
")
periods <- read.table(header=TRUE, text="
COLOR UTC_DateTime
Start_Green 2022-06-15 14:56:22
End_Green 2022-06-15 15:03:08
Start_Red 2022-06-15 15:03:11
End_Red 2022-06-15 15:58:48
")
joined <- read.table(header=TRUE, text="
ID UTC_DateTime Value Color
1 2022-06-15 15:00:00 8 Green
2 2022-06-15 15:05:00 3 Green
3 2022-06-15 15:10:00 2 Red
4 2022-06-15 15:15:00 4 Red
")
Also, if you could somehow incorporate a process which converts periods
to periods_wide
that would be great, see:
periods_wide <- read.table(header=TRUE, text="
COLOR UTC_DateTime_Start UTC_DateTime_End
Green 2022-06-15 14:56:22 2022-06-15 15:03:08
Red 2022-06-15 15:03:11 2022-06-15 15:58:48
")
Please be aware that there might be many more instances and also many more periods (of the same or other colors)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于
powerjoin :: Power_inner_join
和lubridate
的可能解决方案。请注意,read.table
没有按预期读取数据,因此,我使用read.csv
:A possible solution, based on
powerjoin::power_inner_join
andlubridate
. Notice thatread.table
was not reading the data as intended and, therefore, I usedread.csv
: