我有一个沿横断面收集的图像数据框。我们的想法是我们想要在图像中随机抽样(每个横断面大约1000个)。我们希望通过在移动窗口中随机选择它们来做到这一点(包括20/30秒,或每10-15条记录)。因此,在任务的前20秒内选择1张图像,从21-40秒选择1张,依此类推。
有用的字段包括:
Date.Time(factor)格式:20161102 101440
Date.Time.Local(POSIXct)格式:2016-11-02 10:14:40
Mission.Time(int):192(进入任务的秒数,对于可用图像不会从1开始,每次上升2-3次)
FILENAME(因子):20161102_hb01_0049.jpg
如果可以吐出随机选择的记录选择的文件名,这将是一个额外的奖励。
任何帮助都会很棒,因为我现在只能通过定义分别选择每一个的范围来考虑非常冗长的方法,但这也不能在横断面上重现
谢谢,太棒了!它似乎适用于选择一个图像数量的窗口,但不适用于应用于我的数据的时间,因为它们不会以相等的间隔上升。
头(多个)
FileName日期时间
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
答案 0 :(得分:0)
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)