From 8161e617ee66923e4df7d460b4406e30f8d9c094 Mon Sep 17 00:00:00 2001 From: topepo Date: Thu, 4 Apr 2019 09:11:01 -0400 Subject: [PATCH 1/2] additions for #165 --- NAMESPACE | 1 + R/adds.R | 20 ++++++++++++++++++++ man/add_rowindex.Rd | 20 ++++++++++++++++++++ tests/testthat/test_adds.R | 22 ++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 R/adds.R create mode 100644 man/add_rowindex.Rd create mode 100644 tests/testthat/test_adds.R diff --git a/NAMESPACE b/NAMESPACE index f85992519..f24856ce0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,6 +76,7 @@ export(.preds) export(.x) export(.y) export(C5.0_train) +export(add_rowindex) export(boost_tree) export(check_empty_ellipse) export(decision_tree) diff --git a/R/adds.R b/R/adds.R new file mode 100644 index 000000000..9a2cc73b4 --- /dev/null +++ b/R/adds.R @@ -0,0 +1,20 @@ +#' Add a column of row numbers to a data frame +#' +#' @param x A data frame +#' @return The same data frame with a column of 1-based integers named `.row`. +#' @examples +#' mtcars %>% add_rowindex() +#' @export +#' @importFrom dplyr mutate +add_rowindex <- function(x) { + if (!is.data.frame(x)) { + stop("`x` should be a data frame.", call. = FALSE) + } + if (nrow(x) > 0) { + x <- dplyr::mutate(x, .row = 1:nrow(x)) + } else { + x$.row <- integer(0) + } + x +} + diff --git a/man/add_rowindex.Rd b/man/add_rowindex.Rd new file mode 100644 index 000000000..b8599cf2d --- /dev/null +++ b/man/add_rowindex.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/adds.R +\name{add_rowindex} +\alias{add_rowindex} +\title{Add a column of row numbers to a data frame} +\usage{ +add_rowindex(x) +} +\arguments{ +\item{x}{A data frame} +} +\value{ +The same data frame with a column of 1-based integers named \code{.row}. +} +\description{ +Add a column of row numbers to a data frame +} +\examples{ +mtcars \%>\% add_rowindex() +} diff --git a/tests/testthat/test_adds.R b/tests/testthat/test_adds.R new file mode 100644 index 000000000..e693cc973 --- /dev/null +++ b/tests/testthat/test_adds.R @@ -0,0 +1,22 @@ +library(dplyr) + +# ------------------------------------------------------------------------------ + +context("adding functions") +source("helpers.R") + +# ------------------------------------------------------------------------------ + +test_that('adding row indicies', { + iris_2 <- iris %>% add_rowindex() + expect_true(nrow(iris_2) == 150) + expect_true(sum(names(iris_2) == ".row") == 1) + expect_true(is.integer(iris_2$.row)) + + mtcar_2 <- dplyr::as_tibble(mtcars) %>% dplyr::slice(0) %>% add_rowindex() + expect_true(nrow(mtcar_2) == 0) + expect_true(sum(names(mtcar_2) == ".row") == 1) + expect_true(is.integer(mtcar_2$.row)) + + expect_error(as.matrix(mtcars) %>% add_rowindex()) +}) From 5422387aca6af4f24d0fb156e5ddf31c51071774 Mon Sep 17 00:00:00 2001 From: topepo Date: Thu, 4 Apr 2019 09:38:29 -0400 Subject: [PATCH 2/2] travis update removing 3.1 https://www.tidyverse.org/articles/2019/04/r-version-support/ --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e45533ed3..f7f839773 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ sudo: true warnings_are_errors: false r: - - 3.1 - 3.2 - oldrel - release @@ -17,7 +16,6 @@ r: matrix: allow_failures: - - r: 3.1 - r: 3.2 r_binary_packages: