【发布时间】:2016-03-15 22:35:18
【问题描述】:
我有一组具有不同采样间隔的动物位置。我想要做的是分组和序列,其中采样间隔匹配某个标准(例如低于某个值)。让我用一些虚拟数据来说明:
start <- Sys.time()
timediff <- c(rep(5,3),20,rep(5,2))
timediff <- cumsum(timediff)
# Set up a dataframe with a couple of time values
df <- data.frame(TimeDate = start + timediff)
# Calculate the time differences between the rows
df$TimeDiff <- c(as.integer(tail(df$TimeDate,-1) - head(df$TimeDate,-1)),NA)
# Define a criteria in order to form groups
df$TimeDiffSmall <- df$TimeDiff <= 5
TimeDate TimeDiff TimeDiffSmall
1 2016-03-15 23:11:49 5 TRUE
2 2016-03-15 23:11:54 5 TRUE
3 2016-03-15 23:11:59 20 FALSE
4 2016-03-15 23:12:19 5 TRUE
5 2016-03-15 23:12:24 5 TRUE
6 2016-03-15 23:12:29 NA NA
在这个虚拟数据中,行 1:3 属于一组,因为它们之间的时间差 TimeDiffSmall 等于FALSE)。
结合来自两个多个 SO 答案的信息(例如 part 1),我创建了一个解决此问题的函数。
number.groups <- function(input){
# part 1: numbering successive TRUE values
input[is.na(input)] <- F
x.gr <- ifelse(x <- input == TRUE, cumsum(c(head(x, 1), tail(x, -1) - head(x, -1) == 1)),NA)
# part 2: including last value into group
items <- which(!is.na(x.gr))
items.plus <- c(1,items+1)
sel <- !(items.plus %in% items)
sel.idx <- items.plus[sel]
x.gr[sel.idx] <- x.gr[sel.idx-1]
return(x.gr)
# Apply the function to create groups
df$Group <- number.groups(df$TimeDiffSmall)
TimeDate TimeDiff TimeDiffSmall Group
1 2016-03-15 23:11:49 5 TRUE 1
2 2016-03-15 23:11:54 5 TRUE 1
3 2016-03-15 23:11:59 20 FALSE 1
4 2016-03-15 23:12:19 5 TRUE 2
5 2016-03-15 23:12:24 5 TRUE 2
6 2016-03-15 23:12:29 NA NA 2
这个功能实际上可以解决我的问题。这就是,这似乎是一种疯狂而新手的做法。有什么功能可以更专业的解决我的问题吗?
【问题讨论】:
-
cumsum(c(TRUE, diff(df$TimeDate) > 5))是否为您的更大示例做此操作?