[R] Help finding the mode and maximum for a specified 'window' of time series data

From: Staples, Angela Dawn <adstaple_at_indiana.edu>
Date: Sun, 15 Jun 2008 12:10:06 -0400


I am relatively new to R, so apologize up front for my long question, particularly if there is too much or too little information.

I have a large time series data set where each subject's behavior was originally coded on .25s intervals for 3min task. I am trying to determine if the findings are different depending on the coding interval (i.e. Compare .25s to 1s to 5s to 10s).

I also need to see if it makes a difference if I use the maximal value within the interval or the modal value within the interval. In other words, I do not need the average value.

Here a sample of my data with time as the first column:

sample <-cbind((1:20)*.25,c(1,1,1,2,2,3,3,3,4,5,6,4,4,3,3,3,2,1,1,1))

      [,1] [,2]

 [1,] 0.25    1
 [2,] 0.50    1
 [3,] 0.75    1
 [4,] 1.00    2
 [5,] 1.25    2
 [6,] 1.50    3
 [7,] 1.75    3
 [8,] 2.00    3
 [9,] 2.25    4

[10,] 2.50 5
[11,] 2.75 6
[12,] 3.00 4
[13,] 3.25 4
[14,] 3.50 3
[15,] 3.75 3
[16,] 4.00 3
[17,] 4.25 2
[18,] 4.50 1
[19,] 4.75 1
[20,] 5.00 1

I need help returning the maximum and minimum of a specified "window" such that the function/loop/etc would return:

maximum (window=4, or 1s) would be (2, 3, 6, 4, 2) mode (window=4, or 1s) would be (1, 3, 4, 3, 2)

Given the coding conventions in this research area, I need the values to be from adjacent, as opposed to overlapping, windows. There are likely to be situations where there is no clear mode (1,1,2,2). In those cases it would be fine to have 1.5 or 2 returned, but not NA. The data file is in the long format with each subject having 720 rows of data. I've tried playing with the row indices, but I cannot figure out how to 'move' the window.

I would appreciate any help/suggestions. Since I'm new to this, the suggestion doesn't have to be pretty, I just need it to work.

Sincerely,

Angela



Angela Staples
Doctoral Candidate
Psychological and Brain Sciences
Indiana University
1101 E. 10th St.
Bloomington, IN 47405
http://www.indiana.edu/~batessdl/

The plural of anecdote is not data.
~ Roger Brinner



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 15 Jun 2008 - 16:39:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 15 Jun 2008 - 19:30:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive