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, ...)
object |
a data frame or an object inheriting from the |
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 |
an optional character vector of names or column numbers of character variables not to be converted to factors. |
exclude.unique |
if |
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 |
... |
not used. |
a data frame with (some) character variables replaced by corresponding factors.
John Fox jfox@mcmaster.ca
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))
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.