Learn R Programming

car (version 3.1-0)

strings2factors: Convert Character-String Variables in a Data Frame to Factors

Description

Converts the character variables (or a subset of these variables) in a data frame to factors, with optional control of the order of the resulting factor levels.

Usage

strings2factors(object, which, not, exclude.unique, levels, verbose, ...)
# S3 method for data.frame
strings2factors(object, which, not, 
    exclude.unique=TRUE, levels=list(), verbose=TRUE, ...)

Value

a data frame with (some) character variables replaced by corresponding factors.

Arguments

object

a data frame or an object inheriting from the "data.frame" class.

which

an optional character vector of names or column numbers of the character variables to be converted to factors; if absent, all character variables will be converted, except as excluded by the not and exclude.unique arguments (see below).

not

an optional character vector of names or column numbers of character variables not to be converted to factors.

exclude.unique

if TRUE (the default), character variables all of whose values are unique (i.e., all different from each other) are not converted to factors. Such variables, which would have as many levels as there are cases, are typically case identifiers and not categorical variables. If FALSE, character variables all of whose values are unique are converted to factors with a warning.

levels

an optional named list, each element of which is a character vector of levels of the corresponding factor. This argument allows you to control the order of levels of the factor; if omitted, or if a particular factor is omitted from the list, the levels will be in the default alphabetic order.

verbose

if TRUE (the default), the names of the character variables that were converted to factors are printed on the console.

...

not used.

Author

John Fox jfox@mcmaster.ca

See Also

Examples

Run this code
M <- Moore # from the carData package
M$partner <- as.character(Moore$partner.status)
M$fcat <- as.character(Moore$fcategory)
M$names <- rownames(M) # values are unique
str(M)
str(strings2factors(M))
str(strings2factors(M,
  levels=list(partner=c("low", "high"), fcat=c("low", "medium", "high"))))
str(strings2factors(M, which="partner", levels=list(partner=c("low", "high"))))
str(strings2factors(M, not="partner", exclude.unique=FALSE))

Run the code above in your browser using DataLab