Learn R Programming

dataPreparation (version 0.4.3)

setColAsNumeric: Set columns as numeric

Description

Set as numeric a character column (or a list of columns) from a data.table.

Usage

setColAsNumeric(dataSet, cols, stripString = FALSE, verbose = TRUE)

Arguments

dataSet

Matrix, data.frame or data.table

cols

List of column(s) name(s) of dataSet to transform into numerics

stripString

should I change "," to "." in the string? (logical, default to FALSE) If set to TRUE, computation will be a bit longer

verbose

Should the function log (logical, default to TRUE)

Value

dataSet (as a data.table), with specified columns set as numeric.

Examples

Run this code
# NOT RUN {
# Build a fake data.table
dataSet <- data.frame(charCol1 = c("1", "2", "3"), 
						 charCol2 = c("4", "5", "6"))

# Set charCol1 and charCol2 as numeric
dataSet <- setColAsNumeric(dataSet, cols = c("charCol1", "charCol2"))

# Using strip string when spaces or wrong decimal separator is used
dataSet <- data.frame(charCol1 = c("1", "2", "3"), 
                      charCol2 = c("4, 1", "5, 2", "6, 3"))

# Set charCol1 and charCol2 as numeric
setColAsNumeric(dataSet, cols = c("charCol1", "charCol2")) 
# generate mistakes
setColAsNumeric(dataSet, cols = c("charCol1", "charCol2"), stripString = TRUE) 
# Doesn't generate any mistake (but is a bit slower)
# }

Run the code above in your browser using DataLab