/
where-na.R
99 lines (72 loc) · 1.69 KB
/
where-na.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#' Which rows and cols contain missings?
#'
#' Internal function that is short for `which(is.na(x), arr.ind = TRUE)`.
#' Creates array index locations of missing values in a dataframe.
#'
#' @param x a dataframe
#'
#' @return a matrix with columns "row" and "col", which refer to the row and
#' column that identify the position of a missing value in a dataframe
#'
#' @seealso [which_na()]
#'
#' @examples
#'
#' where_na(airquality)
#' where_na(oceanbuoys$sea_temp_c)
#'
#' @export
#'
where_na <- function(x){
which(is.na(x), arr.ind = TRUE)
}
#' Which elements contain missings?
#'
#' Equivalent to `which(is.na())` - returns integer locations of missing values.
#'
#' @param x a dataframe
#'
#' @return integer locations of missing values.
#'
#' @seealso [where_na()]
#'
#' @examples
#'
#' which_na(airquality)
#'
#' @export
#'
which_na <- function(x){
which(is.na(x))
}
#' Which variables contain missing values?
#'
#' It can be helpful when writing other functions to just return the names
#' of the variables that contain missing values. `miss_var_which` returns a
#' vector of variable names that contain missings. It will return NULL when
#' there are no missings.
#'
#' @param data a data.frame
#'
#' @return character vector of variable names
#'
#' @export
#'
#' @examples
#' miss_var_which(airquality)
#'
#' miss_var_which(mtcars)
#'
miss_var_which <- function(data){
# basic type tests
test_if_dataframe(data)
test_if_missing(data)
test_if_null(data)
# if there are no missing values, return NULL
if (!anyNA(data)) {
return(NULL)
}
# else return variables that contain ANY missing values
na_vars <- names(which(colSums(is.na(data)) > 0))
return(na_vars)
}