在时间段内按时间戳加入

发布于 2025-02-09 08:32:12 字数 1211 浏览 1 评论 0原文

我有两个桌子,我想在日期加入。如果一个实例属于颜色期,我希望该实例将上述颜色作为变量接收(请参阅加入):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情出卖 2025-02-16 08:32:12

基于powerjoin :: Power_inner_joinlubridate的可能解决方案。请注意,read.table没有按预期读取数据,因此,我使用read.csv

library(tidyverse)
library(powerjoin)
library(lubridate)

instance <- read.csv(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
", strip.white = T)

periods <- read.csv(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
", strip.white = T)

periods %>% 
  mutate(COLOR = str_remove(COLOR, "Start_|End_"), 
         name = rep(c("d1", "d2"), 2)) %>% 
  pivot_wider(COLOR, values_from = UTC_DateTime) %>% 
  power_inner_join(instance,., 
                   by = c(~ ymd_hms(.x$UTC_DateTime) <= ymd_hms(.y$d2),  
                          ~ ymd_hms(.x$UTC_DateTime) >= ymd_hms(.y$d1))) %>% 
  select(-(d1:d2))

#>   ID        UTC_DateTime Value COLOR
#> 1  1 2022-06-15 15:00:00     8 Green
#> 2  2 2022-06-15 15:05:00     3   Red
#> 3  3 2022-06-15 15:10:00     2   Red
#> 4  4 2022-06-15 15:15:00     4   Red

A possible solution, based on powerjoin::power_inner_join and lubridate. Notice that read.table was not reading the data as intended and, therefore, I used read.csv:

library(tidyverse)
library(powerjoin)
library(lubridate)

instance <- read.csv(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
", strip.white = T)

periods <- read.csv(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
", strip.white = T)

periods %>% 
  mutate(COLOR = str_remove(COLOR, "Start_|End_"), 
         name = rep(c("d1", "d2"), 2)) %>% 
  pivot_wider(COLOR, values_from = UTC_DateTime) %>% 
  power_inner_join(instance,., 
                   by = c(~ ymd_hms(.x$UTC_DateTime) <= ymd_hms(.y$d2),  
                          ~ ymd_hms(.x$UTC_DateTime) >= ymd_hms(.y$d1))) %>% 
  select(-(d1:d2))

#>   ID        UTC_DateTime Value COLOR
#> 1  1 2022-06-15 15:00:00     8 Green
#> 2  2 2022-06-15 15:05:00     3   Red
#> 3  3 2022-06-15 15:10:00     2   Red
#> 4  4 2022-06-15 15:15:00     4   Red
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文