You can use which
and then tabulate, order, etc.
all <- which(data>5, arr.ind=TRUE)
table(all[,"row"])
all[order(all[,"row"]),]
split(all, all[,"row"])
EDIT: For the rolling mean, you can calculate the rolling mean first and then procede as mentioned above.
sra <- apply(data, 2, rollmean, k=5)
all <- which(sra>5, arr.ind=TRUE)
EDIT2: You can also get the dates, if you use rownames(all).
table(rownames(all))
split(all, rownames(all))
EDIT3: Apparently I missunderstood the question. The problem with the names comes from the apply
function. If you use lapply
instead, you get the desired rownames. Then you can cbind
it with the data to get NA's for the first and last 2 days.
sra <- do.call(cbind, lapply(data, rollmean, k=5))
sra <- cbind(sra, data)[, 1:ncol(sra)]
all <- which(sra>data, arr.ind=TRUE)
all
EDIT4: Note that rollmean has an align-argument. You apparently want to right-align (default is center).
sra <- do.call(cbind, lapply(data, rollmean, k=5, align="right"))
sra <- cbind(sra, data)[, 1:ncol(sra)]
all <- which(sra>data, arr.ind=TRUE)
all
EDIT 5: If sra
is of class xts
, it does not have rownames and the matrix all
consequently does not either. You can just use as.matrix(sra)
to get rownames again. The final line I added is just in case you want to know the names of the columns instead of the number.
sra <- do.call(cbind, lapply(data, rollmean, k=5, align="right"))
sra <- as.matrix(cbind(sra, data)[, 1:ncol(sra)])
all <- which(sra>data, arr.ind=TRUE)
table(rownames(all))
split(all[,"col"], rownames(all))
lapply(split(all[,"col"], rownames(all)), function(x) colnames(data)[x])
EDIT 6: To look at one particular date, save the final list and specify the date and then extract the date from your list. For instance:
lst <- lapply(split(all[,"col"], rownames(all)), function(x) colnames(data)[x])
dat <- "2013-07-23"
lst[dat]