strings2factors {car}R Documentation

Convert Character-String Variables in a Data Frame to Factors


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.


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



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


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).


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


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.


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.


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


not used.


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


John Fox

See Also

factor, data.frame


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
  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))

[Package car version 3.1-0 Index]