0

My vector contains the frequency per day of a certain event in a certain month.

I want to see what run of 16 days contains the highest frequency, and I would like to extract the dates which start en and it.

vector=table(date[year(date)==2001&month(date)==05])

I know how to do this, but my method is (obviously) too primitive.

max(c(sum(vector[1:16]),sum(vector[2:17]),sum(vector[3:18]),sum(vector[4:19]),sum(vector[5:20]),sum(vector[6:21]))/sum(vector))

Edit: For reproducibility the data in vector is provided in .csv form below:

"","Var1","Freq"
"1","2001-05-06",1
"2","2001-05-08",1
"3","2001-05-09",7
"4","2001-05-10",2
"5","2001-05-11",10
"6","2001-05-12",10
"7","2001-05-13",7
"8","2001-05-14",20
"9","2001-05-15",24
"10","2001-05-16",15
"11","2001-05-17",27
"12","2001-05-18",17
"13","2001-05-19",13
"14","2001-05-20",15
"15","2001-05-21",13
"16","2001-05-22",26
"17","2001-05-23",17
"18","2001-05-24",19
"19","2001-05-25",7
"20","2001-05-26",5
"21","2001-05-27",6
"22","2001-05-28",2
"23","2001-05-29",1
"24","2001-05-31",1
s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • Please provide a reproducible example http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – akrun Apr 01 '15 at 19:10
  • Or just see `zoo::rollmean`. Or window functions in `dplyr`. – Gregor Thomas Apr 01 '15 at 19:12
  • rollmean is working miracles, now trying to figure how to extract the date. – s_baldur Apr 01 '15 at 19:31
  • Found something that works for the time being: `x=rollmean(vector,16,align=c("left"))` `sort(unique(date[year(date)==2001&month(date)==05]))[ seq(along=x)[ar == max(x)]] ` – s_baldur Apr 01 '15 at 20:21

1 Answers1

1

Assuming the data in vector is as shown in your data example, something like

max_start <- which.max(rollmean(vector$Freq, 16, align="left"))
date_max_start <- vector$Var1[max_start] 
date_max_end <- vector$Var1[max_start + 16] 
WaltS
  • 5,410
  • 2
  • 18
  • 24