as.numeric_discrete: Discrete (factor or character) to numeric vector

View source: R/helpers.R

as.numeric_discreteR Documentation

Discrete (factor or character) to numeric vector

Description

Converts a factor with numeric levels, or, a character vector with numeric values to a numeric vector using as.numeric_factor, or, as.numeric_character respectively. If an unnamed numeric vector is passed, it will be returned as a named numeric vector, if this vector is continuous, it will be returned discretised (by calling ts_discrete), the labels will be rounded to 'signif(x, digits = 4).

Usage

as.numeric_discrete(x, keepNA = FALSE, sortUnique = FALSE)

Arguments

x

A factor with levels that are numeric, or, a character vector representing numbers.

keepNA

Keep NA values (TRUE), or remove them (default = FALSE)

sortUnique

Should the unique character/factor level values be sorted? (default = FALSE)

Value

A named numeric vector with original factor levels / character values / numeric values as names.

Examples


# Continuous
i <- runif(10,0,9)
as.numeric_discrete(i)

# Integer
as.numeric_discrete(round(i))

# Factor with NAs
f <- factor(c(round(runif(9,0,9)),NA))
as.numeric_discrete(f)
as.numeric_discrete(f, keepNA = FALSE)

# Character vector
c <- c("Thank","you", "for", "the flowers")
as.numeric_discrete(c)
as.numeric_discrete(c, sortUnique = TRUE)

c <- c("Thank","you", "for", "the", "flowers")
as.numeric_discrete(c)
as.numeric_discrete(c, sortUnique = TRUE)



FredHasselman/casnet documentation built on April 20, 2024, 3:05 p.m.