Skip to content

Commit

Permalink
fix: Relocate .after when .after is the last column works
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaneastwood committed Apr 13, 2020
1 parent d20b522 commit 1c10f16
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions R/relocate.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ relocate <- function(.data, ..., .before = NULL, .after = NULL) {
where <- 1L
col_pos <- union(col_pos, where)
}

lhs <- setdiff(seq(1L, where - 1L), col_pos)
rhs <- setdiff(seq(where + 1L, ncol(.data)), col_pos)
col_pos <- unique(c(lhs, col_pos, rhs))
col_pos <- col_pos[col_pos <= length(data_names)]

res <- .data[unique(c(lhs, col_pos, rhs))]
res <- .data[col_pos]
if (has_groups(.data)) res <- set_groups(res, group_vars(.data))
res
}
9 changes: 8 additions & 1 deletion inst/tinytest/test_relocate.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ expect_equal(
info = "Test .after functionality for multiple variables"
)

expect_equal(
iris %>% relocate(contains("Petal"), .after = Species),
iris[, c("Sepal.Length", "Sepal.Width", "Species", "Petal.Length", "Petal.Width")],
info = "Test .after works when .after is the last column"
)

expect_error(
mtcars %>% relocate(gear, .after = mpg, .before = cyl)
mtcars %>% relocate(gear, .after = mpg, .before = cyl),
info = "relocate() fails when .after and .before are both given"
)

0 comments on commit 1c10f16

Please sign in to comment.