File tree Expand file tree Collapse file tree 4 files changed +63
-0
lines changed Expand file tree Collapse file tree 4 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,7 @@ export(.preds)
7676export(.x)
7777export(.y)
7878export(C5.0_train)
79+ export(add_rowindex)
7980export(boost_tree)
8081export(check_empty_ellipse)
8182export(decision_tree)
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ })
You can’t perform that action at this time.
0 commit comments