Skip to content

Commit

Permalink
Added tidiers for matrix objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrtwo committed Feb 17, 2015
1 parent 6ac6356 commit f1e35cf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ S3method(glance,glmnet)
S3method(glance,htest)
S3method(glance,kmeans)
S3method(glance,lm)
S3method(glance,matrix)
S3method(glance,merMod)
S3method(glance,nls)
S3method(glance,pyears)
Expand Down Expand Up @@ -71,6 +72,7 @@ S3method(tidy,kmeans)
S3method(tidy,lm)
S3method(tidy,manova)
S3method(tidy,map)
S3method(tidy,matrix)
S3method(tidy,merMod)
S3method(tidy,nls)
S3method(tidy,pairwise.htest)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ broom 0.3.5.9000

* Fixed bug in `tidy.pairwise.htest`, which now can handle cases where the grouping variable is numeric.
* Added `tidy.aovlist` method. This added `stringr` package to IMPORTS to trim whitespace from the beginning and end of the `term` and `stratum` columns. This also required adjusting `tidy.aov` so that it could handle strata that are missing p-values.
* Set up `glance.lm` to work with `aov` objects along with `lm` objects.
* Added `tidy` and `glance` for matrix objects, with `tidy.matrix` converting a matrix to a data frame with rownames included, and `glance.matrix` returning the same result as `glance.data.frame`.

broom 0.3.5
-----------
Expand Down
45 changes: 45 additions & 0 deletions R/matrix_tidiers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Tidiers for matrix objects
#'
#' These perform tidying operations on matrix objects. \code{tidy} turns the
#' matrix into a data.frame while bringing rownames, if they exist, in as
#' a column called \code{.rownames} (since results of tidying operations never
#' contain rownames). \code{glance} simply reports the number of rows and
#' columns. Note that no augment method exists for matrices.
#'
#' @param x A matrix
#' @param ... extra arguments, not used
#'
#' @examples
#'
#' mat <- as.matrix(mtcars)
#' tidy(mat)
#' glance(mat)
#'
#' @name matrix_tidiers


#' @rdname matrix_tidiers
#'
#' @return \code{tidy.matrix} returns the original matrix converted into
#' a data.frame, except that it incorporates rownames (if they exist)
#' into a column called \code{.rownames}.
#'
#' @export
tidy.matrix <- function(x, ...) {
fix_data_frame(x, newcol = ".rownames")
}


#' @rdname matrix_tidiers
#'
#' @return \code{glance} returns a one-row data.frame with
#' \item{nrow}{number of rows}
#' \item{ncol}{number of columns}
#' \item{complete.obs}{number of rows that have no missing values}
#' \item{na.fraction}{fraction of values across all rows and columns that
#' are missing}
#'
#' @export
glance.matrix <- function(x, ...) {
glance.data.frame(x)
}
42 changes: 42 additions & 0 deletions man/matrix_tidiers.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/matrix_tidiers.R
\name{matrix_tidiers}
\alias{glance.matrix}
\alias{matrix_tidiers}
\alias{tidy.matrix}
\title{Tidiers for matrix objects}
\usage{
\method{tidy}{matrix}(x, ...)

\method{glance}{matrix}(x, ...)
}
\arguments{
\item{x}{A matrix}

\item{...}{extra arguments, not used}
}
\value{
\code{tidy.matrix} returns the original matrix converted into
a data.frame, except that it incorporates rownames (if they exist)
into a column called \code{.rownames}.

\code{glance} returns a one-row data.frame with
\item{nrow}{number of rows}
\item{ncol}{number of columns}
\item{complete.obs}{number of rows that have no missing values}
\item{na.fraction}{fraction of values across all rows and columns that
are missing}
}
\description{
These perform tidying operations on matrix objects. \code{tidy} turns the
matrix into a data.frame while bringing rownames, if they exist, in as
a column called \code{.rownames} (since results of tidying operations never
contain rownames). \code{glance} simply reports the number of rows and
columns. Note that no augment method exists for matrices.
}
\examples{
mat <- as.matrix(mtcars)
tidy(mat)
glance(mat)
}

0 comments on commit f1e35cf

Please sign in to comment.