R - return datatable row number of max or min value in sliding window -
i trying retrieve row number associated max/min value in sliding-window. i'm subsetting row number retrieve value different column.
per request, here dput(head(dataframe3)):
structure(list(time = c("00:00:01|", "00:00:03|", "00:00:04|", "00:00:05|", "00:00:06|", "00:00:07|"), average = c(8, 5.75, 5.33333333333333, 5.23076923076923, 5.15, 5.15), negativechange = c(-3, -0.75, -0.333333333333333, -0.230769230769231, -0.15, -0.15), positivechange = c(0, 0, 0.107843137254902, 0.210407239819005, 0.291176470588235, 0.291176470588235)), .names = c("time", "average", "negativechange", "positivechange"), class = c("data.table", "data.frame"), row.names = c(na, -6l), .internal.selfref = <pointer: 0x0000000001300788>)
here upload of truncated text data file, , subsequently, code used import r , point trying code end timestamp portion:
code:
#prepare workspace...delete prior data , values rm(list=ls()) #load packages library(data.table) library(dplyr) #set working directory, setwd(filepath) setwd() #load fixed width data, n = 39 dataframe <- read.fwf("dataframe3_truncated.txt", widths = c(9,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), header = false, sep = "\t", skip = 4, na.strings = c("-")) #transform data.table dataframe <- data.table(dataframe) #calculate row averages, adding "average" column data set dataframe2 <- dataframe[, .(average = rowmeans(.sd, na.rm = true)), "v1"] #calculate negativechange , positivechange using subscript method negativechange <- numeric(nrow(dataframe2)) positivechange <- numeric(nrow(dataframe2)) (i in 1:(nrow(dataframe2)-90)) { y <- x <- i+90 negativechange[i] <- min(dataframe2$average[y:x]) - dataframe2$average[i] positivechange[i] <- max(dataframe2$average[y:x]) - dataframe2$average[i] } #add negativechange , positivechange columns datatable dataframe3 <- dataframe2[, .(time = as.character(v1), average, negativechange, positivechange)] dataframe3
positivechange maximum positive deviation average[i] in [i:i + 90]. negativechange maximum negative deviation average[i] in [i:i + 90]. endtimestamp, , why i'm trying find row number max/min value, should max the timestamp @ average highest [i:i + 90] , min the timestamp @ average lowest [i:i + 90]. tried endtimestamp feature trigger when positivechange >= 1, or negativechange <= -1.
examples of code i've tried find max value row number below:
#which.max endtimestamp <- numeric(nrow(dataframe3)) (i in seq(nrow(dataframe3))) { x <- (i + 1) y <- (i + 91) z <- (i) if (dataframe3$positivechange[i] >= 1) { endtimestamp[i] <- dataframe3[(which.max(dataframe3$average[x:y]) + z), time] } else { endtimestamp[i] <- na } } #which timestamprowindex <- c() timestamprowactual <- c() endtimestamp3 <- numeric(nrow(dataframe3)) (i in seq(nrow(dataframe3))) { x <- (i + 1) y <- (i + 91) z <- (i) if (dataframe3$positivechange[i] >= 1) { timestamprowindex <- append(timestamprowindex, which(dataframe3$average[x:y] == max(dataframe3$average[x:y]))) timestamprowactual <- timestamprowindex[length(timestamprowindex)] + z endtimestamp3[i] <- dataframe3[as.integer(timestamprowactual), time] } else { endtimestamp3[i] <- na } }
neither solution works finding max positivechange >= 1, , seem worse when transformed find min negativechange <= -1. due nature of data, sequential rows wherein average >= 1 should in cases have same end timestamp. code above produces ascending timestamps. , @ 1 point (rows 928 through 973) produces descending timestamps, makes no sense.
i'm sure there simple answer, being new r , coding generally, i've spent hours trying find no avail.
also, z variable correct fact , which.max functions return index value based on range being examined (i think). if max value in 500:600 in 504 row of datatable, functions return value of 4. there way around that, return 504?
suggestions? happy provide more info if needed.
i don't understand purpose of code, why need moving window aggregates? maybe there data structure more suitable problem. however, using given data, suggest following:
"negativechange" minimum deviance average in given interval. per definition minimum value per interval. looking minmal (maximal) values in moving window. package rcpproll provieds usefull functions task:
library(rcpproll) dataframe2$min_average = roll_minl(average, 90) dataframe2$max_average = roll_maxl(average, 90)
in next step trying row number (or position in interval?) of min/max value per interval if need information have use loop.
#calculate row averages, adding "average" column data set dataframe2 <- dataframe[, .(average = rowmeans(.sd, na.rm = true)), "v1"] # calculate min/max of rolling window (i in 1:nrow(dataframe2)) { j = min(i+90, nrow(dataframe2)) # upper bound of window dataframe2$min_average[i] = min(dataframe2$average[i:j]) dataframe2$pos_min_average[i] = (i-1) + which.min(dataframe2$average[i:j]) dataframe2$max_average[i] = max(dataframe2$average[i:j]) dataframe2$pos_max_average[i] = (i-1) + which.max(dataframe2$average[i:j]) }
Comments
Post a Comment