Learn R Programming

netCoin (version 2.1.49)

dichotomize: Dichotomize

Description

Converts factor or character columns of a data frame into a set of dichotomous (dummy/indicator) columns. Their names will correspond to the unique labels or texts of every category discovered across the selected variables. If multiple columns are provided, categories are unified per row.

Usage

dichotomize (data, variables, sep = "", regex = FALSE, 
               text = c("none", "lower", "upper"),
               min = 1, length = 0, sort = TRUE,
               values = NULL, stopwords = NULL, nas = "None",
               sparse = FALSE, add = FALSE)

Value

A data frame composed of the original variables plus the added dichotomous columns, or a sparse matrix if sparse = TRUE.

Arguments

data

A data frame containing the factor or textual columns to be converted. Cells can contain simple values or multiple events delimited by a separator.

variables

A character vector of column names to be converted into dichotomous vectors.

sep

A character string (or vector) used to divide columns with multiple events. If this separator is "" (default), every unique cell is treated as a single category. Use "C" to concatenate multiple columns before dichotomization.

regex

Logical. If FALSE (default), sep is matched exactly. If TRUE, sep is treated as a regular expression (e.g., "\\s+" to split by whitespace).

text

Character. Text transformation option. "none" (default) leaves text intact. "lower" or "upper" converts text to lower or upper case, respectively, and completely removes punctuation and numbers.

min

Integer or numeric. Converts to dichotomous vectors only the labels or texts that have a frequency greater than or equal to the value of this parameter. If the value is between 0 and 1, it is interpreted as a percentage of the total rows.

length

Integer. The strict maximum number of dichotomous columns generated, prioritizing the most frequent categories. 0 implies no limit.

sort

Logical. If TRUE, the new columns are ordered by their frequencies in descending order.

values

A character vector of specific labels or texts strictly selected for conversion into dichotomous columns.

stopwords

Character vector. An optional list of words to be removed from the text before dichotomization. Only applied if text is set to "lower" or "upper".

nas

Character. Variable name used to account for NA (missing) values across the set of variables. Set to NULL to ignore missing values.

sparse

Logical. If TRUE, returns a sparse matrix (requires the Matrix package) instead of a data.frame.

add

Logical. If TRUE, appends the newly generated dichotomous columns to the original input data.frame.

Author

Modesto Escobar, Department of Sociology and Communication, University of Salamanca, and Luis Martinez Uribe, Fundacion Juan March.

References

Escobar, M. and Martinez-Uribe, L. (2020) Network Coincidence Analysis: The netCoin R Package. Journal of Statistical Software, 93, 1-32. tools:::Rd_expr_doi("10.18637/jss.v093.i11").

Examples

Run this code
  # A character column
  frame1 <- data.frame(A = c("Man", "Women", "Man", "Undet."))
  dichotomize(frame1, "A", sep = "; ")
  
  # A character column (with separator)
  frame2 <- data.frame(A = c("Man; Women", "Women; Women",
                             "Man; Man", "Undet.; Women; Man"))
  dichotomize(frame2, "A", sep = "; ")
  
  # A character column and another factor column (same separator)
  frame3 <- data.frame(A = c("Man; Women", "Women; Women",
                             
                             "Man; Man", "Undet.; Women; Man"),
                       C = factor(c(1:4), labels = c("Paris", "New York",
                                                     "London; New York", "")))
  dichotomize(frame3, c("A", "C"), sep = "; ")
  
  # A set of simple character or factor (same levels) variables.
  # In this case, you must use "C" separator.
  frame4 <- data.frame(A = c("Man", "Women","Man", "Undet",NA),
                       B = c("Women","Women","Man","Women",NA),
                       C = c(NA,NA,NA,"Man",NA))
  dichotomize(frame4, c("A","B","C"), sep="C")
  
  # Text mining example: Splitting by whitespace, removing punctuation/numbers, 
  # transforming to lowercase and filtering stopwords
  frame5 <- data.frame(text_col = c("I love apples and 10 oranges!", 
                                    "Apples are great.", 
                                    "I hate bananas."))
  dichotomize(frame5, "text_col", sep = "\\s+", regex = TRUE, 
              text = "lower", stopwords = c("i", "and", "are"))

Run the code above in your browser using DataLab