Learn R Programming

datacheck (version 1.2.2)

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; has_punct; is.onlyLowers; is.properName; is.withinRange; is_one_of; is_only_lowers; is_proper_name; is_within_range

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_one_of("m", "m") == TRUE

is_one_of("m", c("f", "m")) == TRUE

is_one_of("y", c("f", "m")) == FALSE

is_one_of(c("b", "c", "d"), c("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_one_of("A", "sample.csv") == TRUE
is_one_of("Z", "sample.csv") == FALSE

# switching back to your working directory
setwd(owd)
 

Run the code above in your browser using DataLab