patternapply

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

commit 30cce81953407be5510d127089fd5a240f6f659d
parent c858ad82b888c55529d15e2d8c1c1acb66b57c74
Author: eamoncaddigan <eamon.caddigan@gmail.com>
Date:   Mon, 22 Feb 2016 17:06:09 -0500

My first attempt at S3 generics.

Diffstat:
AR/generics.R | 31+++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+), 0 deletions(-)

diff --git a/R/generics.R b/R/generics.R @@ -0,0 +1,31 @@ + +#' Convert a replacement_list object into a character matrix. +#' +#' @param x A replacement_list. +#' +#' @return A character matrix. Each row corresponds to an entry in the +#' replacement_list. +as.matrix.replacement_list <- function(x) { + rlMatrix <- matrix(NA_character_, + nrow = length(x), + ncol = length(attr(x, "col_names"))) + colnames(rlMatrix) <- attr(x, "col_names") + + # Loop (I know) through the elements in the replacement list and copy them + # over to the new matrix. + # XXX: This will need to be changed if named vectors are recognized by + # patternapply(). + for (i in seq_along(x)) { + rlMatrix[i, seq_along(x[[i]])] <- x[[i]] + } +} + +#' Convert a replacement_list object into a data.frame. +#' +#' @param x A replacement_list. +#' +#' @return A data.frame. Each row corresponds to an entry in the +#' replacement_list. +as.data.frame.replacement_list <- function(x) { + return(as.data.frame.matrix(as.matrix.replacement_list(x))) +}