-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathn-prop-miss-complete.R
122 lines (115 loc) · 1.97 KB
/
n-prop-miss-complete.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#' Return the number of missing values
#'
#' Substitute for `sum(is.na(data))`
#'
#' @param x a vector
#'
#' @return numeric the number of missing values
#'
#' @export
#'
#' @examples
#'
#' n_miss(airquality)
#' n_miss(airquality$Ozone)
#'
n_miss <- function(x){
test_if_null(x)
sum(is.na(x))
}
#' Return the number of complete values
#'
#' A complement to `n_miss`
#'
#' @param x a vector
#'
#' @return numeric number of complete values
#'
#' @export
#'
#' @examples
#'
#' n_complete(airquality)
#' n_complete(airquality$Ozone)
#'
n_complete <- function(x){
test_if_null(x)
# number of total elements - number of missings
sum(!is.na(x))
}
#' Return the proportion of missing values
#'
#' This is shorthand for `mean(is.na(x))`
#'
#' @param x vector or data.frame
#'
#' @return numeric the proportion of missing values in x
#'
#' @export
#'
#' @examples
#'
#' prop_miss(airquality)
#' prop_miss(airquality$Ozone)
#'
prop_miss <- function(x){
test_if_null(x)
mean(is.na(x))
}
#' Return the percent of missing values
#'
#' This is shorthand for `mean(is.na(x)) * 100`
#'
#' @param x vector or data.frame
#'
#' @return numeric the percent of missing values in x
#'
#' @export
#'
#' @examples
#'
#' pct_miss(airquality)
#' pct_miss(airquality$Ozone)
#'
pct_miss <- function(x){
prop_miss(x) * 100
}
#' Return the proportion of complete values
#'
#' The complement to `prop_miss`
#'
#' @param x vector or data.frame
#'
#' @return numeric proportion of complete values
#'
#' @export
#'
#' @examples
#'
#' prop_complete(airquality)
#' prop_complete(airquality$Ozone)
#'
prop_complete <- function(x){
test_if_null(x)
# 1 - proportion of missings
1 - mean(is.na(x))
}
#' Return the percent of complete values
#'
#' The complement to `pct_miss`
#'
#' @param x vector or data.frame
#'
#' @return numeric percent of complete values
#'
#' @export
#'
#' @examples
#'
#' pct_complete(airquality)
#' pct_complete(airquality$Ozone)
#'
pct_complete <- function(x){
test_if_null(x)
prop_complete(x) * 100
}