patternapply

Iteratively try patterns against a character vector.
git clone https://git.eamoncaddigan.net/patternapply.git
Log | Files | Refs | README | LICENSE

generics.R (1184B)


      1 
      2 #' Convert a replacement_list object into a character matrix.
      3 #'
      4 #' @param x A replacement_list.
      5 #'
      6 #' @return A character matrix. Each row corresponds to an entry in the
      7 #'   replacement_list.
      8 #'   
      9 #' @export
     10 #'
     11 #' @method as.matrix replacement_list
     12 as.matrix.replacement_list <- function(x) {
     13   rlMatrix <- matrix(NA_character_,
     14                      nrow = length(x),
     15                      ncol = length(attr(x, "col_names")))
     16   colnames(rlMatrix) <- attr(x, "col_names")
     17 
     18   # Loop (I know) through the elements in the replacement list and copy them
     19   # over to the new matrix.
     20   # XXX: This will need to be changed if named vectors are recognized by
     21   # patternapply().
     22   for (i in seq_along(x)) {
     23     rlMatrix[i, seq_along(x[[i]])] <- x[[i]]
     24   }
     25 
     26   return(rlMatrix)
     27 }
     28 
     29 #' Convert a replacement_list object into a data.frame.
     30 #'
     31 #' @param x A replacement_list.
     32 #'
     33 #' @return A data.frame. Each row corresponds to an entry in the
     34 #'   replacement_list.
     35 #'
     36 #' @export
     37 #' 
     38 #' @method as.data.frame replacement_list
     39 as.data.frame.replacement_list <- function(x) {
     40   return(as.data.frame.matrix(as.matrix.replacement_list(x),
     41                               stringsAsFactors = FALSE))
     42 }