R/parseMonths.R

Defines functions parseMonths

Documented in parseMonths

#'
#'@title Convert character vector of dates to numeric vector of months
#'
#'@description Function to convert a character vector of dates to a numeric vector of months.
#'
#'@param x - the character vector to convert
#'@param format - the date format (e.g. 'MM/DD/YYYY','DD-MON-YYYY') 
#'
#'@return numeric vector of months (1-12)
#'
#'@details None.
#'
#'@export
#'
parseMonths<-function(x,format='MM/DD/YYYY'){
    r<-vector('numeric',length=length(x));
    if (toupper(format)=='MM/DD/YYYY'){
        sep<-'/';
        xs<-strsplit(x,sep,fixed=TRUE);
        for (i in 1:length(r)) {r[i]<-as.numeric(xs[[i]][1]);}
    } else
    if (any(toupper(format)==c('DD-MON-YYYY','DD-MON-YY'))){
        sep<-'-';
        xs<-strsplit(x,sep,fixed=TRUE);
        MONTH.ABB<-toupper(month.abb);
        for (i in 1:length(r)) {r[i]<-which(toupper(xs[[i]][2])==MONTH.ABB)[1];}
    }    
    return(r)
}
wStockhausen/wtsUtilities documentation built on March 16, 2024, 10:38 a.m.