forked from Al-Murphy/MungeSumstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_empty_cols.R
37 lines (37 loc) · 1.25 KB
/
check_empty_cols.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#' Check for empty columns
#'
#' Empty columns contain only ".", NA, or 0
#' @inheritParams format_sumstats
#' @param sampled_rows First N rows to sample.
#' Set \code{NULL} to use full \code{sumstats_file}.
#' when determining whether cols are empty.
#'
#' @param verbose Print messages.
#'
#' @return empty_cols
#' @keywords internal
#' @importFrom utils head
check_empty_cols <- function(sumstats_dt,
sampled_rows = NULL,
verbose = TRUE) {
if (is.null(sampled_rows)) {
sampled_rows <- nrow(sumstats_dt)
} else {
sampled_rows <- min(sampled_rows, nrow(sumstats_dt))
}
empty_cols <- vapply(
colnames(sumstats_dt), function(x) {
dt <- utils::head(sumstats_dt, sampled_rows)
(sum(unlist(dt[[x]]) != ".") == 0) |
(sum(!is.na(unlist(dt[[x]]))) == 0) |
(sum(unlist(dt[[x]]) != 0) == 0)
},
FUN.VALUE = logical(1)
)
#sometimes NA values appear, they aren't empty so change these to false
empty_cols <- ifelse(is.na(empty_cols),FALSE,empty_cols)
empty_cols <- empty_cols[empty_cols]
messager(length(empty_cols), "empty column(s) detected.",
v=verbose)
return(empty_cols)
}