categorize function

Assign group membership based on another group membership.

Assign group membership based on another group membership.

Given a data frame and a grouping column of type factor or character this function generates a new grouping column which groups the groups.

categorize(df, col, categories, cat.col, keep = TRUE, overwrite = FALSE)

Arguments

  • df: [data.frame]

    Data frame.

  • col: [character(1)]

    Column name of group variable.

  • categories: [list]

    Named list. Names indicate the name of the category while the values are character vectors of values within the range of the col column.

  • cat.col: [character(1)]

    Column name for categorization.

  • keep: [logical(1)]

    Keep the source column col? Default is TRUE.

  • overwrite: [logical(1)]

    If TRUE, cat.col is set to col.

Returns

[data.frame] df = data.frame( group = c("A1", "A1", "A2", "A2", "B1", "B2"), perf = runif(6), stringsAsFactors = FALSE) df2 = categorize(df, col = "group", categories = list(A = c("A1", "A2"), B = c("B1", "B2")), cat.col = "group2")

  • Maintainer: Jakob Bossek
  • License: GPL-3
  • Last published: 2023-03-08