Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions R/new-seq.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#' For Date vectors it's the unique dates;
#' same for hms vectors.
#' For POSIXct vectors the time zone is preserved.
#' For logical objects the longest possible sequence is `c(TRUE, FALSE)`.
#' For logical objects the longest possible sequence is `c(FALSE, TRUE)`
#' so values of `.length_out` greater than 2 are equivalent to 2.
#'
#' @param x The object to generate the sequence from.
#' @param .length_out The maximum length of the sequence.
Expand Down Expand Up @@ -72,7 +73,7 @@
#' new_seq(as.POSIXct(c("2000-01-01 00:00:01", "2000-01-01 00:00:04"),
#' tz = "PST8PDT"
#' ))
#' # for logical objects the longest possible sequence is `c(TRUE, FALSE)`
#' # for logical objects the longest possible sequence is `c(FALSE, TRUE)`
#' new_seq(c(TRUE, TRUE, FALSE), .length_out = 3)
#' @export
new_seq <- function(
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ xnew_data(old_data, int)

By default the sequence depends on the class of the variable:

- logical vectors are length 2 (TRUE and FALSE);
- logical vectors are length 2 (FALSE and TRUE);
- double vectors are 30 equally spaced values from the minimum value to the maximum value;
- integer, Date, POSIXct and hms vectors are up to 30 discrete values from the minimum to the maximum value as evenly spaced as possible;
- character vectors are the number of unique values;
- factor and ordered vectors are the number of levels.

These values can be overridden by setting the following options:

- `new_data.length_out_lgl`, which is 2 by default, for logical vectors;
- `new_data.length_out_lgl`, which is 2 by default, for logical vectors (values greater than 2 are equivalent to 2);
- `new_data.length_out_dbl`, which is 30 by default, for double vectors;
- `new_data.length_out_int`, which is 30 by default, for integer, Date, POSIXct and hms vectors^1;
- `new_data.length_out_chr`, which is Inf by default, for character, factor and ordered vectors.
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ xnew_data(old_data, int)

By default the sequence depends on the class of the variable:

- logical vectors are length 2 (TRUE and FALSE);
- logical vectors are length 2 (FALSE and TRUE);
- double vectors are 30 equally spaced values from the minimum value to
the maximum value;
- integer, Date, POSIXct and hms vectors are up to 30 discrete values
Expand All @@ -101,7 +101,8 @@ By default the sequence depends on the class of the variable:

These values can be overridden by setting the following options:

- `new_data.length_out_lgl`, which is 2 by default, for logical vectors;
- `new_data.length_out_lgl`, which is 2 by default, for logical vectors
(values greater than 2 are equivalent to 2);
- `new_data.length_out_dbl`, which is 30 by default, for double vectors;
- `new_data.length_out_int`, which is 30 by default, for integer, Date,
POSIXct and hms vectors^1;
Expand Down
5 changes: 3 additions & 2 deletions man/new_seq.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-new-seq.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ test_that("new_seq logical", {
expect_identical(new_seq(array(TRUE)), c(FALSE, TRUE))
})

test_that("new_seq logical length_out greater than 2 equivalent to 2", {
x <- c(TRUE, TRUE, FALSE, NA)
expect_identical(new_seq(x, .length_out = 3), c(FALSE, TRUE))
expect_identical(new_seq(x, .length_out = Inf), c(FALSE, TRUE))
expect_identical(
new_seq(x, .length_out = 3, .obs_only = TRUE),
c(FALSE, TRUE)
)
expect_identical(
new_seq(x, .length_out = Inf, .obs_only = TRUE),
c(FALSE, TRUE)
)
expect_identical(new_seq(TRUE, .length_out = 3, .obs_only = TRUE), TRUE)
withr::local_options(new_data.length_out_lgl = 3L)
expect_identical(new_seq(x), c(FALSE, TRUE))
expect_identical(nrow(xnew_data(data.frame(x = x), x)), 2L)
})

test_that("new_seq logical", {
# zero length
expect_identical(new_seq(logical()), c(FALSE, TRUE))
Expand Down
Loading