R/utility_functions.R

Defines functions samp fixstr exchange strswap

samp <- function(x, ex=FALSE) sample(x, size=1)



# remove gaps in strings
fixstr <- function(...)
{
  str = paste(list(...), collapse=" ")
  
  str <- unlist(strsplit(str, " "))
  str <- paste(str[which(str!="")], collapse=" ")
  
  str
}



# currency exchange
exchange <- function(amt, inn, out)
{
  money <- c("cp", "sp", "ep", "gp", "pp")
  conv <- c(1, 10, 50, 100, 500)
  
  inn <- which(money==inn)
  out <- which(money==out)
  
  ret <- floor( amt * conv[inn]/conv[out] )
  if (ret==0)
    stop("Conversion is not possible")
  
  ret
}



# swap 'a' for 'b' in str
strswap <- function(str, a, b)
{
  strs <- unlist(strsplit(str, a))
  for (i in 1:(length(strs)-1)){
    strs[i] <- paste(strs[i], b, sep="")
  }
  
  paste(strs, collapse="")
}
wrathematics/gmhelper documentation built on May 4, 2019, 9:49 a.m.