f_factors: Convert multiple columns to Factors in a data frame

View source: R/ffactors.R

f_factorsR Documentation

Convert multiple columns to Factors in a data frame

Description

Converts multiple specified columns of a data frame into factors. If no columns are specified, it automatically detects and converts columns that are suitable to be factors. The function returns the entire data frame including non factor columns and can report the properties of this new data frame in the console (properties = TRUE).

Usage

f_factors(
  data,
  select = NULL,
  exclude = NULL,
  properties = FALSE,
  force_factors = FALSE,
  ref = NULL,
  unique_num_treshold = 8,
  repeats_threshold = 2,
  ...
)

Arguments

data

A data frame containing the columns to be converted.

select

A character vector specifying the names of the columns to convert into factors. If NULL, the function automatically detects columns that should be factors based on their data type and unique value count. Default is NULL.

exclude

A character vector specifying the names of the columns NOT to convert into factors. If NULL, no columns are excluded. Default is NULL.

properties

Logical. If TRUE, prints a detailed table about the properties of the new data frame to the console. If FALSE no property table will be printed to the console. Default is FALSE.

force_factors

Logical. If TRUE all columns in the data.frame will be converted to factors except for the excluded columns using exclude.

ref

Optional reference level(s) to set after conversion, controlling which level a factor is contrasted against in models (e.g. f_glm, f_lmer). Under R's default treatment contrasts the reference is the first level, which is alphabetical unless set deliberately. Supply either a single string (applied to every converted factor that contains it) or a named character vector mapping column names to reference levels, e.g. ref = c(treatment = "control", dose = "low"). A level that is not present in a given factor is skipped with a warning. Default NULL (leave the reference as the first level).

unique_num_treshold

Numeric. A threshold of the amount of unique numbers a numeric column should have to keep it numeric, i.e. omit factor conversion. Default 8.

repeats_threshold

Numeric. A threshold of the minimal number of repeats a numeric column should have to convert it to a factor. Default 2.

...

Additional arguments passed to the factor() function of baseR.

Details

  • If select is NULL, the function identifies columns with character data or numeric data with fewer than 8 unique values as candidates for conversion to factors.

  • The function checks if all specified columns exist in the data frame and stops execution if any are missing.

  • Converts specified columns into factors, applying any additional arguments provided.

  • Outputs a summary data frame with details about each column, including its type, class, number of observations, missing values, factor levels, and labels.

Value

Returns the modified data frame with the specified (or all suitable) columns converted to factors. Can also force a print of a summary of the data frame's structure to the console (properties = TRUE).

Author(s)

Sander H. van Delden plantmind@proton.me

See Also

factor

Examples

# Make a data.frame:
df <- data.frame(a = c("yes", "no", "yes", "yes", "no",
                       "yes", "yes", "no", "yes"),
                 b = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
                 c = c("apple", "kiwi", "banana", "apple", "kiwi",
                        "banana", "apple", "kiwi", "banana"),
                 d = c(1.1, 1.1, 3.4, 4.5, 5.4, 6.7, 7.8, 8.1, 9.8)
                 )
str(df)

# Convert specified columns to factors:
df1 <- f_factors(df, select = c("a", "c"))
str(df1)


# Convert all potential factor columns to factor but exclude column "b":
df2 <- f_factors(df, exclude = c("b"))
str(df2)

# Convert all columns to factor but exclude column "b":
df3 <- f_factors(df, exclude = c("b"), force_factors = TRUE)
str(df3)

# Or automatically detect and convert suitable columns to factors.
# Thus obtaining the same results as above automatically:
df4 <- f_factors(df)
str(df4)

# Set the reference level of a factor (the level models contrast against).
# Useful before f_glm() / f_lmer(): by default the reference is the first
# (alphabetical) level. Use a named vector to target specific columns:
df1c <- f_factors(df, select = c("a", "c"))
levels(df1c$c)[1]   # default reference is "apple" (alphabetical)
df_ref <- f_factors(df, select = c("a", "c"),
                    ref = c(a = "yes", c = "kiwi"))
levels(df_ref$c)[1] # reference is now "kiwi"
levels(df_ref$a)[1] # reference is now "yes" (was "no")

# In example above col b was converted to a factor as the number of repeats = 2
# and the amount of unique numbers < 8. In order to keep b numeric we can also
# adjust the unique_num_treshold and/or repeats_threshold:
df5 <- f_factors(df, unique_num_treshold = 2)
str(df5)

# Use `properties = TRUE` to view the data frame's structure.
# This forces a printed output which is more insight than standard str() output.
df6 <- f_factors(df, properties = TRUE)


rfriend documentation built on July 7, 2026, 1:06 a.m.