sql - join based on one column and closest time that isn't over -


everything working, slow iterating 2 loops.

basically 2 data frames, 1 , id's , event time. other various ids , readings (value , time stamp) every 10 seconds or so.

i trying join 1 table matching both id, , time @ specific interval before event time, lets 20 seconds.

alternatively data in oracle sql server, if table join's in sql works too.

readingdf <- data.frame(sensorid = c('100001','100001','100001','100001','100002','100002','100002','100002'),                      readtime = as.posixct(c("2017-07-24 04:08:09 edt","2017-07-24 04:08:19 edt",                      "2017-07-24 04:08:29 edt","2017-07-24 04:08:39 edt","2017-07-24 04:08:09 edt","2017-07-24 04:08:19 edt",                      "2017-07-24 04:08:29 edt","2017-07-24 04:08:39 edt"),tz="est"),                      value = c('17.5','15.6','12.9','12.1','22.2', '24.5','19.7','20.1'))   df <- data.frame(sensorid = c('100001','100002','100001','100002','100001','100002','100001','100001'),                      eventtime = as.posixct(c("2017-07-24 04:08:23 edt","2017-07-24 04:08:25 edt","2017-07-24 07:04:40 edt",                      "2017-07-24 02:19:30 edt","2017-07-24 04:37:08 edt","2017-07-24 04:19:59 edt","2017-07-24 03:26:49 edt",                      "2017-07-24 03:58:17 edt"),tz="est")) 

we can create new column, readtime_expand, showing next 20 seconds of readtime. after that, perform join based on sensorid eventtime = readtime_expand. df2 final output.

library(tidyverse)  readingdf2 <- readingdf %>%   mutate(readtime_end = readtime + 20) %>%   mutate(readtime_expand = map2(readtime, readtime_end, function(x, y){     return(seq(x, y, = 1))   })) %>%   unnest()   df2 <- df %>%   left_join(readingdf2, = c("sensorid", "eventtime" = "readtime_expand"))  df2    sensorid           eventtime            readtime value        readtime_end 1    100001 2017-07-24 04:08:23 2017-07-24 04:08:09  17.5 2017-07-24 04:08:29 2    100001 2017-07-24 04:08:23 2017-07-24 04:08:19  15.6 2017-07-24 04:08:39 3    100002 2017-07-24 04:08:25 2017-07-24 04:08:09  22.2 2017-07-24 04:08:29 4    100002 2017-07-24 04:08:25 2017-07-24 04:08:19  24.5 2017-07-24 04:08:39 5    100001 2017-07-24 07:04:40                <na>  <na>                <na> 6    100002 2017-07-24 02:19:30                <na>  <na>                <na> 7    100001 2017-07-24 04:37:08                <na>  <na>                <na> 8    100002 2017-07-24 04:19:59                <na>  <na>                <na> 9    100001 2017-07-24 03:26:49                <na>  <na>                <na> 10   100001 2017-07-24 03:58:17                <na>  <na>                <na> 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -