0

I want to create a double sliding window in a for loop. An example data set might look like:

    a <- structure(list(a = c(0.0961136, 0.1028192, 0.1106424, 0.1106424, 
0.117348, 0.117348, 0.117348, 0.122936, 0.1307592, 0.1307592, 
0.1318768, 0.1318768, 0.1385824, 0.1385824, 0.1318768, 0.1251712, 
0.1251712, 0.1251712, 0.1251712, 0.1251712)), .Names = "a", row.names = c(NA, 
-20L), class = "data.frame")

The code I have so far looks like this:

windowSize <- 5
windowStep <- 1
dat <- list()
for (i in seq(from = 1, to = nrow(a), by = windowStep)){
  window1 <- a[i:windowSize, ]
  window2 <- a[i:windowSize + windowSize, ]
  if (median(window1) <= 0.12 && (median(window1) >= 0.08)) {
    p <- "True"
 } else 
    p <- "not"

  dat[[i]] <- c(p)
}  
result <- as.data.frame(do.call(rbind, dat)) 

This example shows that I require two windows of size 5 (data points) to slide one in front of the other by 1 data point at a time. This example does not utilize window 2 because it doesn't work!(I will need it to work eventually) However using just window1 to calculate the median (in this case) at each step works but the output is incorrect. The if statements ask that if the median of window 1 is between 0.08 and 0.12 then output "True" else "not."

Output for my for loop =

1  True
2  True
3  True
4  True
5  True
6  True
7  True
8  True
9  True
10  not
11  not
12  not
13  not
14  not
15  not
16  not
17  not
18  not
19  not
20  not

Correct output as checked using rollapply (and obviously can be seen by eye)

rollapply(a, 5, FUN = median, by = 1, by.column = TRUE, partial = TRUE, align = c("left"))

should be:

1  True
2  True
3  True
4  not
5  not
6  not
7  not
8  not
9  not
10 not
11 not
12 not
13 not
14 not
15 not
16 not
17 not
18 not
19 not
20 not

Could the solution remain as a for loop if possible as I have much more to add but need to get this right first. Thanks.

PharmR
  • 260
  • 1
  • 3
  • 12
  • 1
    Maybe this can help you: http://stackoverflow.com/questions/35039361/summing-the-counts-in-a-data-frame-using-sliding-window/35040537#35040537 – Jaap Feb 03 '16 at 18:54
  • Thanks but ideally I want to keep it as a for loop because i'd eventually like to add more criteria to the windows. – PharmR Feb 03 '16 at 19:02
  • 1
    hint: `i` goes from `1` to `nrow(a)`, your subsetting operator goes from `i` to `windowSize`. What happens when `i > windowSize`? – Vlo Feb 03 '16 at 19:46

1 Answers1

0

This gets close..modified from: https://stats.stackexchange.com/questions/3051/mean-of-a-sliding-window-in-r

windowSize <- 10
windowStep <- 1
Threshold <- 0.12
a <- as.vector(a)
data <- a

slideFunct <- function(data, windowSize, WindowStep){
  total <- length(data)
  dataLength <- seq(from=1, to=(total-windowSize), by=windowStep)
  result <- vector(length = length(dataLength))
  for(i in 1:length(dataLength)){
    result[i] <- if (median(data[dataLength[i]:(dataLength[i]+windowSize)]) <= Threshold)
      result[i] <- "True"
    else
      result[i] <- "not"
  }
  return(result)
}
Community
  • 1
  • 1
PharmR
  • 260
  • 1
  • 3
  • 12