-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented
set_n_miss
and set_prop_miss
- resolves #298 - added imports, `vctrs` and `cli` - which are both free dependencies as they are used within the already used tidyverse already.
- Loading branch information
Showing
8 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#' Set a proportion or number of missing values | ||
#' | ||
#' @param x vector of values to set missing | ||
#' @param prop proportion of values between 0 and 1 to set as missing | ||
#' | ||
#' @return vector with missing values added | ||
#' @name set-prop-n-miss | ||
#' | ||
#' @examples | ||
#' vec <- rnorm(5) | ||
#' set_prop_miss(vec, 0.2) | ||
#' set_prop_miss(vec, 0.4) | ||
#' set_n_miss(vec, 1) | ||
#' set_n_miss(vec, 4) | ||
#' @export | ||
set_prop_miss <- function(x, prop = 0.1) { | ||
check_is_scalar(prop) | ||
check_btn_0_1(prop) | ||
x[sample(seq_along(x) <= prop * length(x))] <- NA | ||
x | ||
} | ||
|
||
#' @rdname set-prop-n-miss | ||
#' @param n number of values to set missing | ||
#' @export | ||
set_n_miss <- function(x, n = 1) { | ||
check_is_scalar(n) | ||
check_is_integer(n) | ||
x[sample(seq_along(x) <= n)] <- NA | ||
x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# set_n_miss errors appropriately | ||
|
||
`x` must be greater than 0 | ||
`x` is -1 | ||
|
||
--- | ||
|
||
Can't convert `x` <character> to <integer>. | ||
|
||
--- | ||
|
||
Can't convert from `x` <double> to <integer> due to loss of precision. | ||
* Locations: 1 | ||
|
||
--- | ||
|
||
`x` must be length 1 | ||
`x` is 1.5 and 2, and `x` has length: 2 | ||
|
||
# set_prop_miss errors appropriately | ||
|
||
`prop` must be between 0 and 1 | ||
`prop` is -1 | ||
|
||
--- | ||
|
||
`prop` must be between 0 and 1 | ||
`prop` is a | ||
|
||
--- | ||
|
||
`prop` must be between 0 and 1 | ||
`prop` is 1.5 | ||
|
||
--- | ||
|
||
`x` must be length 1 | ||
`x` is 1.5 and 2, and `x` has length: 2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
vec <- 1:10 | ||
|
||
test_that("set_n_miss works", { | ||
expect_equal(n_miss(set_n_miss(vec, 1)), 1) | ||
expect_equal(n_miss(set_n_miss(vec, 5)), 5) | ||
expect_equal(n_miss(set_n_miss(vec, 10)), 10) | ||
expect_equal(n_miss(set_n_miss(vec, 0)), 0) | ||
}) | ||
|
||
test_that("set_prop_miss works", { | ||
expect_equal(prop_miss(set_prop_miss(vec, 0.1)), .1) | ||
expect_equal(prop_miss(set_prop_miss(vec, 0.5)), .5) | ||
expect_equal(prop_miss(set_prop_miss(vec, 1)), 1) | ||
expect_equal(prop_miss(set_prop_miss(vec, 0)), 0) | ||
}) | ||
|
||
test_that("set_n_miss errors appropriately", { | ||
expect_snapshot_error( | ||
set_n_miss(vec, -1) | ||
) | ||
expect_snapshot_error( | ||
set_n_miss(vec, "a") | ||
) | ||
expect_snapshot_error( | ||
set_n_miss(vec, 1.5) | ||
) | ||
expect_snapshot_error( | ||
set_n_miss(vec, c(1.5, 2)) | ||
) | ||
}) | ||
|
||
test_that("set_prop_miss errors appropriately", { | ||
expect_snapshot_error( | ||
set_prop_miss(vec, -1) | ||
) | ||
expect_snapshot_error( | ||
set_prop_miss(vec, "a") | ||
) | ||
expect_snapshot_error( | ||
set_prop_miss(vec, 1.5) | ||
) | ||
expect_snapshot_error( | ||
set_prop_miss(vec, c(1.5, 2)) | ||
) | ||
}) |