flag_weighted {flagr} | R Documentation |
Flag aggregation by the weighted frequency method
Description
This method can be used when you want to derive the flag of an aggregate that is a weighted average, index, quantile, etc.
Usage
flag_weighted(i, f, w)
Arguments
i |
An integer column identifier of data.frame or a matrix containing the flags and weights used to derived the flag for the aggregates. |
f |
A data.frame or a matrix containing the flags of the series (one column per period) |
w |
A data.frame or a matrix with same size and dimesion as |
Value
flag_weighted
Returns a character vector with the flag that has the highest weighted frequency or multiple flags in alphabetical
order (in case there are more than one flag with the same highest weight) as the first value, and the sum of weights for the given flag(s) as
the second value for the given columns of f,w
defined by the parameter i
.
See Also
Examples
flag_weighted(1,
data.frame(f=c("pe","b","p","p","u","e","d"), stringsAsFactors = FALSE),
data.frame(w=c(10,3,7,12,31,9,54)))
flag_weighted(1,
data.frame(f=c("pe","b","p","p","up","e","d"), stringsAsFactors = FALSE),
data.frame(w=c(10,3,7,12,31,9,54)))
flag_weighted(1,
data.frame(f=c("pe",NA,"pe",NA,NA,"d"), stringsAsFactors = FALSE),
data.frame(w=c(10,3,7,12,31,9)))
flags <- tidyr::spread(test_data[, c(1:3)], key = time, value = flags)
weights <- tidyr::spread(test_data[, c(1, 3:4)], key = time, value = values)
flag_weighted(7,flags[, c(2:ncol(flags))],weights[, c(2:ncol(weights))])
weights<-apply(weights[, c(2:ncol(weights))],2,function(x) x/sum(x,na.rm=TRUE))
weights[is.na(weights)] <- 0
flags<-flags[, c(2:ncol(flags))]
sapply(1:ncol(flags),flag_weighted,f=flags,w=weights)