replace_na_zero_missing: Perform in-place zero/na/missing replacements on a...

Description Usage Arguments Details Value Examples

View source: R/replace_na_zero_missing.R

Description

Perform an operation of choice on a data.table (in-place): (i) replace na/inf values (ii) replace zero values (iii) replace missing values (e.g. " ").

Usage

1
2
replace_na_zero_missing(data, replace, replace_with = "DEFAULT",
  col = names(data))

Arguments

data

Data.table which is to be modified in place (data.table).

replace

Value used to replace the existing values (character) [Default: Mode-specific (see above)]

col

Vector of column names which are to be modified (vector - character) [Default: All columns in the data].

mode

Value which is to be replaced ((i) 'na_inf' (replace: Na, inf, -inf -> 0) (ii) 'zero' (replace: 0 -> NA), (iii) 'missing' (replace: " "* -> NA) (character).

Details

Maintained by: Clara Marquardt

Value

data.table modified in place

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
sample_data <- copy(ehR_cohort)
sample_data[, ':='(test_col=prediction, test_col_1=feature_categorical_3)]

## replace: na_inf
sample_data[c(1,3,4), ':='(test_col=c(NA, -Inf, NA), test_col_1=NA)]
print(sample_data)
replace_na_zero_missing(data=sample_data, replace="na_inf", col=c("test_col"))
print(sample_data)

## replace: zero
sample_data[c(1,3,4), ':='(test_col=0, test_col_1=0)]
print(sample_data)
replace_na_zero_missing(data=sample_data, replace="zero", col=c("test_col_1"))
print(sample_data)
## mode - missing
sample_data[, ':='(test_col=as.character(test_col), test_col_1=as.character(test_col_1))]
sample_data[c(1,3,4), ':='(test_col=" ", test_col_1="     ")]
print(sample_data)
replace_na_zero_missing(data=sample_data, replace="missing")
print(sample_data)

ClaraMarquardt/ehR documentation built on Sept. 17, 2017, 6:05 p.m.