我正在处理来自神经生理学记录的时间序列数据,这些记录通常具有标记事件开始的“标记”(例如,在屏幕上显示的刺激)。我试图根据特定的标记对特定的窗口/时代进行子集,然后对这些单独的窗口/时代进行平均。
为了说明这一点,下面是一个非常简单的例子(我的实际数据集有数百万个数据点,所以最好有高效的解决方案)。
df <- data.frame(value = c(1:10, 101:110), #time series data
marker = c(NA, NA, 'start', NA, NA, NA, NA, 'end', NA, NA, #event markers
NA, NA, 'start', NA, NA, NA, NA, 'end', NA, NA))
start <- which(df$marker == "start") #indices 3 and 13 are the 'start' markers
end <- which(df$marker == 'end') #indices 8 and 18 are the 'end markers'
window1 <- df$value[start[1]:end[1]] #first window (indices 3 to 8)
window2 <- df$value[start[2]:end[2]] #second window (indices 13 to 18)
averageWindow <- (window1 + window2) / 2 #average of the two windows这是否是最有效的方法(我的实际数据中有将近1000个窗口,大约有100万行)?
发布于 2016-03-14 05:03:31
我不确定您想要一个基于所有窗口的平均值还是每个窗口的平均值。所以我决定给出两个结果。使用您的start和end,我用lapply()对数据进行了细分。这时,我删除了不相关的数据。然后,我将列表中的数据帧与rbindlist()组合起来,并将ID分配给一个新列。最后的过程是得到一个平均值。
library(data.table)
start <- which(df$marker == "start") #indices 3 and 13 are the 'start' markers
end <- which(df$marker == 'end') #indices 8 and 18 are the 'end markers'
rbindlist(lapply(1:length(start), function(x){
df[start[x]:end[x], ]}), idcol = TRUE) -> temp
# An overall average
temp[, list(average = sum(value) / uniqueN(.id))]
# average
#1: 333
# An average for each window
temp[, list(average = sum(value) / .N), by = .id]
# .id average
#1: 1 5.5
#2: 2 105.5在回复OP的消息时,我想出了以下代码。我为这6个点中的每个点创建了ID,并计算了每个点的平均值。
temp[, index := 1:.N, by = .id][,
list(average = sum(value) / .N), by = index]
# index average
#1: 1 53
#2: 2 54
#3: 3 55
#4: 4 56
#5: 5 57
#6: 6 58数据
df <- data.frame(value = c(1:10, 101:110), #time series data
marker = c(NA, NA, 'start', NA, NA, NA, NA, 'end', NA, NA, #event markers
NA, NA, 'start', NA, NA, NA, NA, 'end', NA, NA),
stringsAsFactors = FALSE)https://stackoverflow.com/questions/35978270
复制相似问题