naTo0: NA to 0

View source: R/naTo0.R

naTo0R Documentation

NA to 0

Description

Given a set of columns in a data.table, convert all NA values in those columns to 0

Usage

naTo0(data_dt, cols_v = NA, verbose_v = F)

Arguments

data_dt

data.table with NAs in some columns. Data.frames and matrices are accepted.

cols_v

vector of either column indices (numeric) or column names (character) that have NAs to be replaced

verbose_v

boolean values determining if verbose printing should be output.

Value

data.table with same dim(), but NAs are now 0 for all cols_v

Examples

data_df <- data.frame(A = 1:5, B = c(5,4,NA,2,1), C = c(3,NA,2,NA,NA), D = c(NA, NA, 1:3), stringsAsFactors = F)
data_mat <- as.matrix(data_df)
data_dt <- as.data.table(data_df)
naTo0(data_df, cols_v = "D")
naTo0(data_dt, cols_v = colnames(data_dt))
naTo0(data_mat, cols_v = c(1,3))
naTo0(data_df)

weshorton/wrh.rUtils documentation built on Oct. 28, 2024, 7:24 a.m.