-
Notifications
You must be signed in to change notification settings - Fork 28
/
r_na.R
54 lines (48 loc) · 1.14 KB
/
r_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
#' Title
#'
#' Description
#'
#' @param x A \code{\link[base]{data.frame}} or \code{\link[base]{list}} to
#' randomly replace elements with \code{NA}s.
#' @param cols Numeric indices of the columns to incude (use \code{-} to exlcude
#' as well). Default is to assign random \code{NA}s to al columns except the
#' first column.
#' @param prob The proportion of each column/vector elements to assign to
#' \code{NA}.
#' @return Returns a \code{\link[base]{data.frame}} or \code{\link[base]{list}}
#' with random missing values.
#' @keywords na missing
#' @export
#' @examples
#' r_na(mtcars)
#' r_na(mtcars, NULL)
#'
#'
#' library(dplyr)
#'
#' r_data_frame(
#' n = 30,
#' id,
#' race,
#' age,
#' sex,
#' hour,
#' iq,
#' height,
#' died,
#' Scoring = rnorm,
#' Smoker = valid
#' ) %>%
#' r_na(prob=.4)
r_na <- function(x, cols=-1, prob = .05){
if (is.null(cols)) {
x[] <- lapply(x, r_na_vector, prob=prob)
} else {
x[c(cols)] <- lapply(x[c(cols)], r_na_vector, prob=prob)
}
x
}
r_na_vector <- function(x, prob = .05) {
x[sample(seq_along(x), round(prob * length(x)))] <- NA
x
}