-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature suggestion: most()
and assert_count_true()
#539
Comments
Is this suggestion still relevant? |
These are different from some of the typical |
I'm fine with adding
To get the data for all participants for whom most of their Should it take an option cutoff value that defaults to Not trying to muddy the waters, just want to get precise on design and use cases. |
Could you share an example of using |
I like For cleaned_data <-
data |>
mutate(
age =
case_when(
assert_count_true(Person == "Bill" & Age == 40, count = 1) ~ 29, # The fountain of youth :)
TRUE ~ age
)
) My implementation looks something like (if I'm using assert_count_true <- function(x, n = 1) {
stopifnot(is.logical(x))
if (any(is.na(x)) {
stop(deparse(x), " has NA values")
}
if (sum(x) != n) {
stop(deparse(x), " expected ", n, " TRUE values, but ", sum(x), " were found")
}
x
} |
Here's some better, working code for assert_count_true <- function(x, n = 1) {
stopifnot(is.logical(x))
if (any(is.na(x))) {
stop(deparse(substitute(x)), " has NA values")
}
if (sum(x) != n) {
stop_message <-
sprintf(
"`%s` expected %g `TRUE` %s but %g %s found.",
deparse(substitute(x)),
n,
ngettext(n, "value", "values"),
sum(x),
ngettext(sum(x), "was", "were")
)
stop(stop_message)
}
x
}
foo <- c(TRUE, TRUE, FALSE)
assert_count_true(foo, n = 1)
#> Error in assert_count_true(foo, n = 1): `foo` expected 1 `TRUE` value but 2 were found.
bar <- c("Bill", "Sam", "Matan")
assert_count_true(bar == "Bill", n = 1)
#> [1] TRUE FALSE FALSE
bar <- c("Bill", "Sam", "Matan")
assert_count_true(bar == "Bill", n = 2)
#> Error in assert_count_true(bar == "Bill", n = 2): `bar == "Bill"` expected 2 `TRUE` values but 1 was found. Created on 2024-01-31 with reprex v2.0.2 |
I sometimes get dirty data that has multiple values that I need to choose one from. In a recent example, I received a dataset where an individual had multiple values for their sex (both male and female when they definitely did not undergo gender reassignment between the measurements).
To work with these types of issues, I think that two different types of functions can help:
most(x)
is a companion toany()
andall()
from base R. It takes in a vector,x
, and returns true if more than half of the values areisTRUE(x)
.assert_count_true(x, n)
takes in a logical vectorx
and an expected count that should beisTRUE(x)
,n
. Ifsum(isTRUE(x)) == n
, then it returnsx
. If a different number areTRUE
, then it returns an error indicating the mismatch in count.The text was updated successfully, but these errors were encountered: