The problem: actionlist$RPos[1000] has a value of 21. n1 ranges from -31 to 0. When you add them you get a vector with a mix of positive and negative values, which isn’t allowed in subsetting.
How I got there: First check traceback():
traceback()
5: `[.data.frame`(z, actionlist$RPos[i] + n1, j) at #8
4: z[actionlist$RPos[i] + n1, j] at #8
3: is.data.frame(x) at #8
2: cor(z[actionlist$RPos[i] + n1, j], x[, j]) at #8
1: GetTopN(10)
This tells me the problem is in actionlist$RPos[i] + n1 most likely. Then I just added a simple print(i) statement to tell me which iteration was the problem. (Alternatively, you could probably have just checked actionlist$RPos + n1 for trouble spots manually.
For tidyverse users, dplyr gives this error if you try to subset a grouped df, you need to call ungroup first.