Skip to content
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

Add percentage and default sorting to miss_scan_count (Fix #341) #347

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 13 additions & 20 deletions R/miss-scan-count.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,20 @@
#'
#'
miss_scan_count <- function(data,search){
# if there is only one value to search
if (length(search) == 1) {
res <- purrr::map_dfc(data,
~length(grep(search,
x = .))) %>%
# return the dataframe with the columns "
tidyr::pivot_longer(cols = dplyr::everything(),
names_to = "Variable",
values_to = "n")
# but if there are more than one, we need to combine the search terms
}

if (length(search) > 1) {
res <- purrr::map_dfc(data,
~length(grep(paste0(search,
collapse ="|"),
x = .))) %>%
tidyr::pivot_longer(cols = dplyr::everything(),
names_to = "Variable",
values_to = "n")
}
pattern <- paste0(search, collapse = "|")

res <- data %>%
# if the value is in the search terms, return TRUE
dplyr::mutate(dplyr::across(dplyr::everything(), ~grepl(pattern, .x, perl = TRUE))) %>%
# sum the number of times the value is found
dplyr::summarise(dplyr::across(dplyr::everything(), sum)) %>%
# present the data in a long format
tidyr::pivot_longer(cols = dplyr::everything(), names_to = "Variable", values_to = "n") %>%
# calculate the percentage
dplyr::mutate(pct = (n/nrow(data))*100) %>%
# order the data in descending order
dplyr::arrange(-n)

return(res)
}
Expand Down
37 changes: 15 additions & 22 deletions tests/testthat/test-miss-scan-count.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,30 @@ test_that("miss_scan_count returns an error when no data is provided", {
test_that("miss_scan_count returns a data.frame of the right size", {
expect_equal(
dim(miss_scan_count(dat_ms, -99)),
c(3, 2)
c(3, 3)
)
})

test_that("miss_scan_count returns results sorted in descending order by 'n'", {
result <- miss_scan_count(dat_ms, c(-99, -98))
n_values <- result$n
expect_true(all(n_values == sort(n_values, decreasing = TRUE)))
})

correct_answer_1 <- tibble::tribble(
~Variable, ~n,
"x", 1L,
"y", 0L,
"z", 1L
~Variable, ~n, ~pct,
"x", 1L, 20,
"z", 1L, 20,
"y", 0L, 0
)

correct_answer_2 <- tibble::tribble(
~Variable, ~n,
"x", 2L,
"y", 0L,
"z", 2L
~Variable, ~n, ~pct,
"x", 2L, 40,
"z", 2L, 40,
"y", 0L, 0
)

correct_answer_3 <- tibble::tribble(
~Variable, ~n,
"x", 2L,
"y", 1L,
"z", 2L
)

correct_answer_4 <- tibble::tribble(
~Variable, ~n,
"x", 2L,
"y", 1L,
"z", 2L
)

test_that("miss_scan_count returns the right answer", {
expect_equal(miss_scan_count(dat_ms,-99),
Expand Down