impute: Replace missing values in tables and lists

Description Usage Arguments Details Value Note See Also Examples

Description

Replace missing values (NA) in a table and lists

Usage

1
2
3
4
5
6
7
impute(.tbl, .na, ...)

impute_at(.tbl, .na, .vars, ...)

impute_all(.tbl, .na, ...)

impute_if(.tbl, .na, .predicate, ...)

Arguments

.tbl

list-like or table-like structure.

.na

scalar, vector or function as described in na.tools::na.replace()

...

additional args; either a unnamed list of columns (quoted or not) or name=function pairs. See Details.

.vars

character; names of columns to be imputed

.predicate

dply-type predicate functions

Details

impute is similar to other dplyr verbs especially dplyr::mutate(). Like dplyr::mutate() it operates on columns. It changes only missing values (NA) to the value specified by .na.

Behavior:

Behavior depends on the values of .na and ....

impute can be used for three replacement operatations:

  1. impute( .tbl, .na ) : ( missing ... ) Replace missing values in ALL COLS by .na. This is analogous to impute_all.

  2. impute( .tbl, .na, ... ) : ( ... is an unnamed list) Replace column(s) specified in ... by .na. Columns are specified as an unnamed list of quoted or unquoted column names. This is analogous to impute_at where ... specifies .vars

  3. impute( .tbl. col1=na.*, col2=na.* ) : ( missing .na ) : Replace by column-specific .na

Additional arguments are to .na are not used; Use impute_at for this or create your own lambda functions.

impute_all is like impute without specifying .... ... is used for additional arguments to .na

Value

Returns a object as the same type as .tbl. Columns are mutated to replace missing values (NA) with value specied by .na and ...

Note

... is used to specify columns in impute but is used as additional arguments to .na in the other impute_* functions.

See Also

Examples

 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
  data(nacars)
  
## Not run: 
  nacars %>% impute(0, mpg, cyl)
  nacars %>% impute(1:6, mpg, cyl)

  nacars %>% impute( na.mean )
  nacars %>% impute( mean )       # unsafe
  nacars %>% impute( length, mpg, disp )
  nacars %>% impute( mean, mpg, disp )
  nacars %>% impute( mpg=na.mean, cyl=na.max )
  nacars %>% impute( na.mean, c('mpg','disp') )

## End(Not run)

## Not run: 
  nacars %>% impute_at( -99, .vars=1:3 )
  nacars %>% impute_at( .na=na.mean, .vars=1:6 )
  
  # Same, uses `...` for additional args
  nacars %>%   
    impute_at( .na=mean   , .vars=1:6, na.rm = TRUE  )  
  
  nacars %>% impute_at( .na=na.mean, .vars = c('mpg','cyl', 'disp') )

## End(Not run)  


## Not run: 
  nacars %>% impute_all( -99 )
  nacars %>% impute_all( na.min )

## End(Not run)
   

tidyimpute documentation built on May 2, 2019, 3:32 p.m.