Learn R Programming

wakefield (version 0.3.6)

r_na: Replace a Proportion of Values With NA

Description

Replaces a proportion of values with NA. Useful for simulating missing data.

Usage

r_na(x, cols = -1, prob = 0.05)

Arguments

x

A data.frame or list to randomly replace elements with NAs.

cols

Numeric indices of the columns to incude (use - to exlcude as well). Default is to assign random NAs to al columns except the first column.

prob

The proportion of each column/vector elements to assign to NA.

Value

Returns a data.frame or list with random missing values.

Examples

Run this code
# NOT RUN {
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)
# }

Run the code above in your browser using DataLab