R/combineElementsOfVectorsInsideLst_inAvectorsIndexesComplaintWay.R

Defines functions combineElementsOfVectorsInsideLst_inAvectorsIndexesComplaintWay

Documented in combineElementsOfVectorsInsideLst_inAvectorsIndexesComplaintWay

#' Take a list of vectors and combine elements of vectors into new list in a vector's indexes complaint way.
#' 
#' This function takes a list of vectors and combines elements of the vectors from different list elements into their respective vectors in their index complaint way.
#' Vectors are put into a new list which is output. If both list and its vectors are named than input's list elements names become names of the vectors, while input's vectors' names become names of the output list.
#' @param lst A list of vectors. It is advised that list elements and vector elements are named. Vectors must bo of the same length and have the same names (and in the same order)
#' @author Alexander Kheirallah ([email protected])
#' @export

combineElementsOfVectorsInsideLst_inAvectorsIndexesComplaintWay = function(lst){
  # stop if vectors inside the list are NOT of the same length
  stopifnot(allNumericsTheSame(lengths(lst))) 
  # also make sure names of the vectors elements are the same (and in the same order)
  
  lstLenght = length(lst)
  vecLength = length(lst[[1]])
  
  outLst = list()
  
  # for every member of the vectors...
  for(vm in 1:vecLength){
    vectorForOut = c()
    # go through every vector member and collect the data
    for(lm in 1:lstLenght){
      vectorForOut[length(vectorForOut)+1] = lst[[lm]][vm]
    }
    names(vectorForOut) = names(lst)
    outLst = lappend(outLst,vectorForOut)
  }
  names(outLst) = names(lst[[1]])
  outLst
}
msxakk89/dat documentation built on April 3, 2020, 5:39 p.m.