Learn R Programming

easybio (version 1.2.2)

finsert: Create a Vector from an Index-to-Label Map

Description

Constructs a character vector by mapping labels to specified 0-based numeric indices. This is a utility function often used in single-cell analysis to assign cell type annotations to cluster IDs.

Usage

finsert(
  x = list(c(0, 1, 3) ~ "Neutrophil", c(2, 4, 8) ~ "Macrophage"),
  len = integer(),
  setname = TRUE,
  na = "Unknown"
)

Value

A character vector with the specified labels at the given positions. The vector is named with 0-based indices if setname is TRUE.

Arguments

x

The mapping of indices to labels. This can be provided in two formats:

  • A list of formulas, e.g., list(c(0, 1) ~ "LabelA", 2 ~ "LabelB").

  • An expression object, e.g., expression(c(0, 1) == "LabelA", 2 == "LabelB").

len

An optional integer specifying the minimum length of the output vector. If the highest index in x is greater than len, the vector will be automatically extended.

setname

A logical value. If TRUE (the default), the elements of the output vector are named with their corresponding 0-based index (e.g., "0", "1", "2", ...).

na

The character value used to fill positions that are not specified in the mapping. Defaults to "Unknown".

Examples

Run this code
# --- Example 1: Using the default formula list format ---
# This is the recommended and default usage.
mapping_formula <- list(
  c(0, 1, 3) ~ "Neutrophil",
  c(2, 4, 8) ~ "Macrophage"
)
finsert(mapping_formula)

# --- Example 2: Using the expression format for backward compatibility ---
mapping_expr <- expression(
  c(0, 1, 3) == "Neutrophil",
  c(2, 4, 8) == "Macrophage"
)
finsert(mapping_expr, len = 10, na = "Unassigned")

Run the code above in your browser using DataLab