Learn R Programming

datacheck (version 0.9.8)

is.oneOf: Tests if a string or 'factor level' is one of a pre-defined set

Description

The aset parameter may point to a file with level names. This is useful if there are many levels like in database of world countries. The file path may be an absolute one or relative to the current working directory.

Usage

is.oneOf(x, aset)

Arguments

x
a factor level as character string
aset
a vector of character strings or a path to a custom file (full pathname where necessary)

Value

  • boolean TRUE if detects anything

Details

The supporting table must have two columns named 'VALUES' and 'LABELS'. The lookup file must be in comma separated format and using the '.csv' extension. It must also be encoded using UTF-8 character set for being able to use foreign characters across operating systems. This is often an issue when using Excel to develop the file. The x parameter may have just one level or multiple levels separated by ';'. Likewise the aset parameter may have just one level or multiple levels separated by ';'. In any case the x parameter must be a subset of aset (or the lookup file): see the example section.

See Also

Other rule_checks: has.punct, is.onlyLowers, is.properName, is.withinRange

Examples

Run this code
# Case 1: define the reference set or lookup set within the function. Useful for small or 
# binary sets like m(ale)/f(emale)
is.oneOf("m","m") == TRUE

is.oneOf("m","f; m") == TRUE

is.oneOf("y", "f; m") == FALSE

is.oneOf("b;c;d", "a;b;c;d;e") == TRUE


# Case 2: use an external lookup table. The external lookup table must have at least one 
# column called exactly 'VALUES'. May have also another one 'LABELS'. Useful for long 
# lookup tables like list of countries.

# some preparation work for using a temporary directory
owd = getwd()
td = tempdir()
setwd(td)


VALUES = LETTERS[1:10]
LABELS = VALUES
db = cbind(VALUES, LABELS)
db = as.data.frame(db, stringsAsFactors = FALSE)
names(db) = c("VALUES","LABELS")
write.csv(db, 'sample.csv', row.names=FALSE)

is.oneOf("A", "sample.csv") == TRUE
is.oneOf("Z", "sample.csv") == FALSE

# switching back to your working directory
setwd(owd)

Run the code above in your browser using DataLab