R code: .rateYan()
////////////////////////////////

.rateYan<-function(APR,method){
"Objective   : from one APR to another APR and effective rate
          APR : value of the given Annual Percentage Rate
       method : Converting method, e.g., 's2a', 's2q', 's2c' 
                  a for annual
                  s for semi-annual
                  q for quarterly
                  m for monthly
                  d for daily
                  c for continuously
 
  Example 1: > .rateYan(0.1,'s2a')
                                     Two rates
               effective annual rate    0.1025
               APR                      0.1025
 
  Example 2: > .rateYan(0.1,'s2q')
                                      Two rates
           effective quarterly rate  0.02469508
           APR                       0.09878031

 ";.zrateYan(APR,method)}

.zrateYan<-function(APR,rateA2rateB){
  r=rateA2rateB
  if(nchar(r)!=3) stop("Wrong input for the 2nd variable")
  
  if(substr(r,1,1)=='a')        {n_in=1
  } else if(substr(r,1,1)=='s') {n_in=2
  } else if(substr(r,1,1)=='q') {n_in=4
  } else if(substr(r,1,1)=='m') {n_in=12
  } else if(substr(r,1,1)=='d') {n_in=365 
  } else if(substr(r,1,1)=='c') {n_in=1 
  }else{
    return(-99)
  }
  
  if(substr(r,3,3)=='a')        {n_out=1; label='annual'
  } else if(substr(r,3,3)=='s') {n_out=2;  label='semi-annual'
  } else if(substr(r,3,3)=='q') {n_out=4;  label='quarterly'
  } else if(substr(r,3,3)=='m') {n_out=12; label='monthly'
  } else if(substr(r,3,3)=='d') {n_out=365;label='daily'
  } else if(substr(r,3,3)=='c') {n_out=1;  label='continuously'
  }else{
    print('check your 2nd input. Tt should be s2a, s2q, etc')
    return(-99)
  }
  
  x=matrix(1,2)
  n=n_in/n_out
  
    if(substr(r,1,1)=='c') {
    x[1,1]<-exp(APR)^(1/n_out)-1
  } else{
    
    x[1,1]<-(1+APR/n_in)^n-1
  }
  
  if(substr(r,3,3)=='c') {
    x[1,1]<-n_in*log(1+APR/n_in)
  }
  
  x[2,1]<-x[1,1]*n_out
  rownames(x)=c(paste('effective',label,'rate'), 'APR')
  colnames(x)<-"Two rates"
  return(x)
}
///////////////////////////////