mixedData: A Class for Mixed Character and Numeric Data

View source: R/PCSmisc.R

mixedDataR Documentation

A Class for Mixed Character and Numeric Data

Description

The purpose of this class is to represent data that is essentially numeric, but also included some ‘special’ values that are non-numeric and hence the object cannot cannot be represented by a standard numeric class. A typical example would be an assay result that used a symbol like "BQL<(1.00)" to represent left-censored values.

Usage

mixedData(x)
## S3 method for class 'mixedData'
unique(obj)
## S3 method for class 'mixedData'
as.character(obj)
## S3 method for class 'mixedData'
as.double(obj, fill = NA)
## S3 method for class 'mixedData'
as.data.frame(obj, ...)
## S3 method for class 'mixedData'
print(obj)

Arguments

x

A vector containing mixed data (typically character or factor).

obj

An object of class mixedData.

fill

A value to use when no other value is appropriate. See details.

...

Additional arguments to be passed to or from methods.

Details

The class implements just a few methods. The main uses are:

  • as.numeric to avoid the warning issued when calling this method on type character and the danger of calling it on type factor (see Examples).

  • unique, which considers all numeric values as "<Number>" and hence allows one to quickly identify all the ‘special’ (non-numeric) values are.

The print method just prints the character representation but with quote = FALSE for readability.

Value

mixedData returns a new object of class mixedData.

The methods return what one would expect: as.charater, unique and print return character, as.double returns numeric.

Author(s)

Benjamin Rich <mail@benjaminrich.net>

Examples

# Generate a vector with approx. 5% left-censoring:
set.seed(123)
x <- rexp(350, 0.05129329)
x <- ifelse(x < 10, "BQL<(1.00)", as.character(signif(x, 4)))
# Another 'special' result, 'quantity not sufficient':
x[129] <- "QNS"
x <- factor(x)

unique(x)  # Not very useful

# Warning! not the desired result when x is a factor:
as.numeric(x)

# This works, but you get a warning:
as.numeric(as.character(x))

y <- mixedData(x)
unique(y)
print(y)
as.numeric(y)
as.character(y)


benjaminrich/PCSmisc documentation built on Feb. 11, 2024, 9:25 p.m.