Skip to content

Commit

Permalink
Merge pull request #7 from metrumresearchgroup/fix-nasum
Browse files Browse the repository at this point in the history
Fix nasum
  • Loading branch information
michaelmcd18 authored Dec 13, 2022
2 parents fcc7b5d + 8566f33 commit 02cf7fe
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Imports:
hms,
lubridate,
purrr,
readr
readr,
tidyr
Suggests:
sessioninfo,
knitr,
Expand Down
18 changes: 11 additions & 7 deletions R/nasum.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#' Count Number of \code{NA} Values in List Items
#' Display a count of NAs by column in a data.frame
#'
#' @description
#' \code{nasum} Count the number of \code{NA} values in each element of a list-like object.
#'
#' @param x list of vector-like objects; e.g. a data frame
#' @param simplify logical: coerce to vector result if possible
#'
#' @usage nasum(x,simplify=TRUE)
#' @param .df the count of NAs will be calculated for each column in this data
#' set, with any grouping variables retained
#'
#' @details
#' Arguments are passed to \code{simplify}, along with a function
#' that sums instances of \code{NA}.
#'
#' @author Natalie Hsiang
#' @author Tim Waterhouse
#'
#' @examples
#' nasum(Theoph)
#'
#' @export
nasum <- function(x, simplify=TRUE)sapply(x,FUN=function(y)sum(is.na(y)),simplify=simplify)
nasum <- function(.df) {
.df %>%
dplyr::summarize(dplyr::across(dplyr::everything(), ~ sum(is.na(.))), .groups = "keep") %>%
tidyr::pivot_longer(-dplyr::group_cols(), values_to = "n_NA") %>%
dplyr::ungroup() %>%
dplyr::filter(n_NA > 0)
}
6 changes: 5 additions & 1 deletion R/nsub.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
#' @author Samuel P Callisto, PhD
#'
#' @export
nsub <- function(.df, .id="USUBJID") {.df %>% dplyr::distinct({{ .id }}) %>% nrow()}
nsub <- function(.df, .id) {

.df %>% dplyr::distinct({{ .id }}) %>% nrow()

}
11 changes: 5 additions & 6 deletions man/nasum.Rd

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

2 changes: 1 addition & 1 deletion man/nsub.Rd

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

7 changes: 2 additions & 5 deletions tests/testthat/test-nasum.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ Theoph2$Time = NA_real_

test_that("Finds NA within dataframe [MRG-NASM-001]", {
Theoph_na <- nasum(Theoph2)
expect_equal(Theoph_na[1], setNames(3, "Subject"))
expect_equal(Theoph_na[2], setNames(0, "Wt"))
expect_equal(Theoph_na[3], setNames(0, "Dose"))
expect_equal(Theoph_na[4], setNames(132, "Time"))
expect_equal(Theoph_na[5], setNames(5, "conc"))
expect_equal(Theoph_na[1], dplyr::tibble(name = c("Subject", "Time", "conc")))
expect_equal(Theoph_na[2], dplyr::tibble(n_NA = c(3, 132, 5)))
})

0 comments on commit 02cf7fe

Please sign in to comment.