Guest User

Untitled

a guest
Jan 23rd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. pctchange <- function(v) {
  2. # Incremental percent change
  3. (v - c(v[1],v[-(length(v))])) / c(v[1],v[-(length(v))])
  4. }
  5.  
  6. # weighted harmonic mean
  7.  
  8. whmean <- function(v, w){ #, na.rm=FALSE){
  9. #v <- ifelse(na.rm==TRUE, v[!is.na(v)],v )
  10. #w <- ifelse(na.rm==TRUE, w[!is.na(w)],w )
  11. sum(as.numeric(w))/sum(as.numeric(w)/as.numeric(v))
  12. }
  13.  
  14. #weighted mean
  15. wmean <- function(v, w) { #, na.rm=FALSE){
  16. #v <- ifelse(na.rm==TRUE, v[!is.na(v)],v )
  17. #w <- ifelse(na.rm==TRUE, w[!is.na(w)],w )
  18. sum(as.numeric(w)*as.numeric(v))/sum(as.numeric(w))
  19. }
  20.  
  21. # format as percentage
  22. # syntax: >sapply(df[c(...list of columns...),], percentify, r=1)
  23.  
  24. percentify <- function(x, r=1, keep.zeros=FALSE, na.code=""){
  25. ifelse (as.numeric(x) != 0 | keep.zeros==TRUE, paste( format(round ( as.numeric(x) * 100, r) , nsmall=r) , '%', sep='')
  26. ,
  27. na.code)
  28. }
Add Comment
Please, Sign In to add comment