在R中的移动窗口中选择随机数或时间



i具有沿横切收集的图像数据框。这个想法是我们要在图像上随机采样(每个横切约1000)。我们希望通过在移动窗口中随机选择它们(由20/30秒或每10-15个记录组成)来做到这一点。因此,在任务的前20秒中选择1个图像,1个从21-40秒开始,等等。

有用的字段包括:

date.time(因子)格式:20161102 101440
date.time.local(posixct)格式:2016-11-02 10:14:40
Mission.Time(INT):192(进入任务的秒,不会以1次可用图像开始,并且每次都会上升2-3)
文件名(因子):20161102_hb01_0049.jpg

如果可以吐出带有随机选择的记录选择的文件名,这将是一个额外的奖励。

任何帮助都会很棒,因为我目前只能想到通过定义范围单独选择每个范围来实现这一目标的冗长方法,但这也不可在crantects中重现

谢谢,太好了!它似乎可以选择一个图像数量的窗口,但由于将其应用于我的数据时,由于它们不相同的间隔不在我的数据上时不起作用。

头部
文件名dateTime
223 20161102_HB01_FS_0049.JPG 2016-11-02 02:38:03
224 20161102_HB01_FS_0050.JPG 2016-11-02 02:38:05
225 20161102_HB01_FS_0051.JPG 2016-11-02 02:38:08
226 20161102_HB01_FS_0052.JPG 2016-11-02 02:38:10
227 20161102_HB01_FS_0053.JPG 2016-11-02 02:38:13
228 20161102_HB01_FS_0054.JPG 2016-11-02 02:38:15

s<-data.frame(DateTime=seq(as.POSIXct("2016-11-02 10:40:00"), by="sec", length.out=100), FileName=paste0(rep("file-",100), 1:100))
window<-20 
sampleSize <-5
groups<-seq(1, nrow(s), by=window) #indexes of first element in each window
result<-lapply(groups, function(x) s[sample(x:(x+window-1), sampleSize), "FileName"]) #for each group, randomly select sampleSize number of elements

编辑:按时间间隔拆分

s<-data.frame(DateTime=as.POSIXct("2016-11-02 10:40:00") + sample(1:200, 100, replace = T))
s$FileName<-paste0("file-",rownames(s),"-",format(s$DateTime, "%H%M%S"))
s<-s[order(s$DateTime ),] #Order date time from old to recent
sampleSize <-5
window.sec<- 25
split <- seq(min(s$DateTime), max(s$DateTime), by=(window.sec+1)) # splitting into groups 
#[1] "2016-11-02 10:40:02 MYT" "2016-11-02 10:40:28 MYT" "2016-11-02 10:40:54 MYT" "2016-11-02 10:41:20 MYT" "2016-11-02 10:41:46 MYT"
#[6] "2016-11-02 10:42:12 MYT" "2016-11-02 10:42:38 MYT" "2016-11-02 10:43:04 MYT"
groups<- c( sapply(split, function(x) min(which(s$DateTime>=x))) , nrow(s))  #indexes of first element in each group, and include the last index. 
#The first element in each group can be more recent than that in the split, if x is n.
# > s[groups,]
# DateTime       FileName
# 56 2016-11-02 10:40:02 file-56-104002
# 53 2016-11-02 10:40:30 file-53-104030
# 60 2016-11-02 10:40:56 file-60-104056
# 95 2016-11-02 10:41:20 file-95-104120
# 81 2016-11-02 10:41:46 file-81-104146
# 57 2016-11-02 10:42:12 file-57-104212
# 39 2016-11-02 10:42:39 file-39-104239
# 59 2016-11-02 10:43:04 file-59-104304
# 75 2016-11-02 10:43:20 file-75-104320
result<-lapply(1:(length(groups)-1), function(i) s[sample(groups[i]:(groups[i+1]-1), sampleSize), "FileName"])
names(result) <- as.character(split)

最新更新