r - Testing multiple columns in a time series simultaneously -
library("xts") data1<- cbind(a = c(1,2,3,4,5,6,5,4,3,4,5,6,5,4,3,5), b = c(1,2,3,4,5,6,5,4,3,4,5,6,5,4,3,5), c = c(1,2,3,4,5,6,5,4,5,4,5,4,5,4,5,2), d = c(1,2,3,4,5,6,5,4,1,1,1,1,1,2,3,2)) data<- xts(data1, sys.date() - (16:1)) data b c d 2013-07-09 1 1 1 1 2013-07-10 2 2 2 2 2013-07-11 3 3 3 3 2013-07-12 4 4 4 4 2013-07-13 5 5 5 5 2013-07-14 6 6 6 6 2013-07-15 5 5 5 5 2013-07-16 4 4 4 4 2013-07-17 5 3 5 1 2013-07-18 4 4 4 1 2013-07-19 5 5 5 1 2013-07-20 4 6 4 1 2013-07-21 5 5 5 1 2013-07-22 4 4 4 2 2013-07-23 3 3 5 3 2013-07-24 5 5 2 2
i have data set contains 100 such columns. need method or define function can tell me how many such columns are, above 5 days sma (moving average) on given day. if give specific date , 5 days sma, should number of columns above sma and, if possible, column names too.
you can use which
and tabulate, order, etc.
all <- which(data>5, arr.ind=true) table(all[,"row"]) all[order(all[,"row"]),] split(all, all[,"row"])
edit: rolling mean, can calculate rolling mean first , procede mentioned above.
sra <- apply(data, 2, rollmean, k=5) <- which(sra>5, arr.ind=true)
edit2: can dates, if use rownames(all).
table(rownames(all)) split(all, rownames(all))
edit3: apparently missunderstood question. problem names comes apply
function. if use lapply
instead, desired rownames. can cbind
data na's first , last 2 days.
sra <- do.call(cbind, lapply(data, rollmean, k=5)) sra <- cbind(sra, data)[, 1:ncol(sra)] <- which(sra>data, arr.ind=true)
edit4: note rollmean has align-argument. apparently want right-align (default center).
sra <- do.call(cbind, lapply(data, rollmean, k=5, align="right")) sra <- cbind(sra, data)[, 1:ncol(sra)] <- which(sra>data, arr.ind=true)
edit 5: if sra
of class xts
, not have rownames , matrix all
consequently not either. can use as.matrix(sra)
rownames again. final line added in case want know names of columns instead of number.
sra <- do.call(cbind, lapply(data, rollmean, k=5, align="right")) sra <- as.matrix(cbind(sra, data)[, 1:ncol(sra)]) <- 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: @ 1 particular date, save final list , specify date , extract date list. instance:
lst <- lapply(split(all[,"col"], rownames(all)), function(x) colnames(data)[x]) dat <- "2013-07-23" lst[dat]
Comments
Post a Comment