将数据与data.table roll =“ nearest”合并在整个DF中进行匹配

时间:2019-12-06 19:09:27

标签: r data.table

我有两个表,一个样本表和一个消息表。在消息表中,消息记录在跟踪器的采样率之外。我一直在使用data.table roll最近的示例,以使示例消息时间与示例报告中的最接近值相匹配。似乎没有将示例消息返回到最近的时间和所有其他内容的NA,而是似乎将消息滚动到下一条消息为止。


library(data.table)
remotes::install.github("dmirman/gazer") # to get the data
library((gazer)


samp <- system.file("extdata", "TJ_samp.xls", package = "gazer")
samp <- data.table::fread(samp, stringsAsFactors = FALSE) # reads in large datasets 

msg <- system.file("extdata", "TJ_msg.xls", package = "gazer")
msg <- data.table::fread(msg, stringsAsFactors = FALSE) # reads in large datasets 
setDT(samp)
setDT(msg)

DT_mesg <- msg[samp, on="time", roll="nearest"] # use this to get close to values in sample report

DT_mesg
 #SR edfs are a nightmare. This makes it so messages are alined with closest values 
get_msg <- DT_mesg %>% 
  group_by(trial, message) %>% 
  top_n(n=1, wt=desc(time)) # there are a lot of useless messages and they occupy the same time stamp. Only take the first message in time. This was one way I tried to deal with the multiple message issue, but it does not return messages close to their actual time. 

get_msg

1 个答案:

答案 0 :(得分:0)

我能够弄清楚...

setDT(samp)
setDT(msg)

DT_mesg <- msg[samp, on="time", roll=4]

这给了我我想要的结果:


  trial    time                     message i.trial     x     y  pup Label
     1:     1 3314705 !MODE RECORD CR 250 2 1 L\n       1 958.8 580.8 4043  <NA>
     2:    NA 3314709                        <NA>       1 959.1 576.2 4052  <NA>
     3:    NA 3314713                        <NA>       1 959.8 575.6 4053  <NA>
     4:    NA 3314717                        <NA>       1 960.6 575.2 4056  <NA>
     5:    NA 3314721                        <NA>       1 960.2 579.6 4049  <NA>

不确定为什么roll =“ nearest”返回以下内容:

   trial    time                     message i.trial     x     y  pup Label
     1:     1 3314705 !MODE RECORD CR 250 2 1 L\n       1 958.8 580.8 4043  <NA>
     2:     1 3314709 !MODE RECORD CR 250 2 1 L\n       1 959.1 576.2 4052  <NA>
     3:     1 3314713 !MODE RECORD CR 250 2 1 L\n       1 959.8 575.6 4053  <NA>
     4:     1 3314717 !MODE RECORD CR 250 2 1 L\n       1 960.6 575.2 4056  <NA>
     5:     1 3314721 !MODE RECORD CR 250 2 1 L\n       1 960.2 579.6 4049  <NA>