R/fun_bdhLongToWide.R

#' given a list of stocks with data from bdh function, convert the list into a table 
#' cl is the cluster created by doParallel , if this parameter is not given , the function will spawn the cluster itself
#' @export

bdhLongToWide <-function(bdhList, cl){
  if( missing(cl)){
    cl <- makeCluster(detectCores()-1)
    registerDoParallel(cl)
  }


combineDate = foreach(bdh = bdhList) %dopar% {
 as.character( bdh$date)
}
combineDate = sort(as.Date(unique(unlist(combineDate)),format = "%Y-%m-%d"))
convertedBDH  = foreach(bdh = bdhList) %dopar%{
  output = rep(NA, length(combineDate))
  names(output) = combineDate
  output[as.character(bdh$date)] = bdh[,2]
  output
}
result = do.call(cbind,convertedBDH)
colnames(result)  =names(bdhList) 
result
}
davidlamcm/Rtoolbox documentation built on May 15, 2019, 1:14 a.m.