diff --git a/R/new-seq.R b/R/new-seq.R index 5e4b810..bdd6437 100644 --- a/R/new-seq.R +++ b/R/new-seq.R @@ -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. @@ -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( diff --git a/README.Rmd b/README.Rmd index 8426c25..07ace8b 100644 --- a/README.Rmd +++ b/README.Rmd @@ -70,7 +70,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 from the minimum to the maximum value as evenly spaced as possible; - character vectors are the number of unique values; @@ -78,7 +78,7 @@ 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; - `new_data.length_out_chr`, which is Inf by default, for character, factor and ordered vectors. diff --git a/README.md b/README.md index 5b11b25..9849f53 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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; diff --git a/man/new_seq.Rd b/man/new_seq.Rd index ac16651..f990592 100644 --- a/man/new_seq.Rd +++ b/man/new_seq.Rd @@ -144,7 +144,8 @@ For ordered factors the intermediate levels are dropped first. 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 \code{c(TRUE, FALSE)}. +For logical objects the longest possible sequence is \code{c(FALSE, TRUE)} +so values of \code{.length_out} greater than 2 are equivalent to 2. } \section{Methods (by class)}{ \itemize{ @@ -212,7 +213,7 @@ new_seq(hms::as_hms(c("00:00:01", "00:00:04"))) 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) } \seealso{ diff --git a/tests/testthat/test-new-seq.R b/tests/testthat/test-new-seq.R index cbf3972..475d080 100644 --- a/tests/testthat/test-new-seq.R +++ b/tests/testthat/test-new-seq.R @@ -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))