Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pctchange <- function(v) {
- # Incremental percent change
- (v - c(v[1],v[-(length(v))])) / c(v[1],v[-(length(v))])
- }
- # weighted harmonic mean
- whmean <- function(v, w){ #, na.rm=FALSE){
- #v <- ifelse(na.rm==TRUE, v[!is.na(v)],v )
- #w <- ifelse(na.rm==TRUE, w[!is.na(w)],w )
- sum(as.numeric(w))/sum(as.numeric(w)/as.numeric(v))
- }
- #weighted mean
- wmean <- function(v, w) { #, na.rm=FALSE){
- #v <- ifelse(na.rm==TRUE, v[!is.na(v)],v )
- #w <- ifelse(na.rm==TRUE, w[!is.na(w)],w )
- sum(as.numeric(w)*as.numeric(v))/sum(as.numeric(w))
- }
- # format as percentage
- # syntax: >sapply(df[c(...list of columns...),], percentify, r=1)
- percentify <- function(x, r=1, keep.zeros=FALSE, na.code=""){
- ifelse (as.numeric(x) != 0 | keep.zeros==TRUE, paste( format(round ( as.numeric(x) * 100, r) , nsmall=r) , '%', sep='')
- ,
- na.code)
- }
Add Comment
Please, Sign In to add comment