Skip to content

Commit 93ec73f

Browse files
authored
Merge pull request #168 from tidymodels/row-indicies
Row index addition function for #165
2 parents e6f0493 + 5422387 commit 93ec73f

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ sudo: true
88
warnings_are_errors: false
99

1010
r:
11-
- 3.1
1211
- 3.2
1312
- oldrel
1413
- release
@@ -17,7 +16,6 @@ r:
1716

1817
matrix:
1918
allow_failures:
20-
- r: 3.1
2119
- r: 3.2
2220

2321
r_binary_packages:

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export(.preds)
7676
export(.x)
7777
export(.y)
7878
export(C5.0_train)
79+
export(add_rowindex)
7980
export(boost_tree)
8081
export(check_empty_ellipse)
8182
export(decision_tree)

R/adds.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#' Add a column of row numbers to a data frame
2+
#'
3+
#' @param x A data frame
4+
#' @return The same data frame with a column of 1-based integers named `.row`.
5+
#' @examples
6+
#' mtcars %>% add_rowindex()
7+
#' @export
8+
#' @importFrom dplyr mutate
9+
add_rowindex <- function(x) {
10+
if (!is.data.frame(x)) {
11+
stop("`x` should be a data frame.", call. = FALSE)
12+
}
13+
if (nrow(x) > 0) {
14+
x <- dplyr::mutate(x, .row = 1:nrow(x))
15+
} else {
16+
x$.row <- integer(0)
17+
}
18+
x
19+
}
20+

man/add_rowindex.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_adds.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
library(dplyr)
2+
3+
# ------------------------------------------------------------------------------
4+
5+
context("adding functions")
6+
source("helpers.R")
7+
8+
# ------------------------------------------------------------------------------
9+
10+
test_that('adding row indicies', {
11+
iris_2 <- iris %>% add_rowindex()
12+
expect_true(nrow(iris_2) == 150)
13+
expect_true(sum(names(iris_2) == ".row") == 1)
14+
expect_true(is.integer(iris_2$.row))
15+
16+
mtcar_2 <- dplyr::as_tibble(mtcars) %>% dplyr::slice(0) %>% add_rowindex()
17+
expect_true(nrow(mtcar_2) == 0)
18+
expect_true(sum(names(mtcar_2) == ".row") == 1)
19+
expect_true(is.integer(mtcar_2$.row))
20+
21+
expect_error(as.matrix(mtcars) %>% add_rowindex())
22+
})

0 commit comments

Comments
 (0)