relabel_class {cola}R Documentation

Relabel class IDs according to the reference ID

Description

Relabel class IDs according to the reference ID

Usage

relabel_class(class, ref, full_set = union(class, ref), return_map = TRUE)

Arguments

class

A vector of class IDs.

ref

A vector of reference IDs.

full_set

The full set of ID levels.

return_map

Whether return the mapping or the adjusted labels.

Details

In partition, the exact value of the class ID is not of importance. E.g. for two partitions a, a, a, b, b, b, b and b, b, b, a, a, a, a, they are the same partitions although the labels of a and b are switched in the two partitions. Here relabel_class function switches the labels in class vector according to the labels in ref vector to maximize sum(class == ref).

Mathematically, this is called linear sum assignment problem and it is solved by solve_LSAP.

Value

A named vector where names correspond to the IDs in class and values correspond to ref, which means map = relabel_class(class, ref); map[class] returns the relabelled IDs.

The returned object attaches a data frame with three columns:

If return_map in the relabel_class is set to FALSE, the function simply returns a vector of adjusted class IDs.

If the function returns the mapping vector (when return_map = TRUE), the mapping variable is always character, which means, if your class and ref are numeric, you need to convert them back to numeric explicitely. If return_map = FALSE, the returned relabelled vector has the same mode as class.

Examples

class = c(rep("a", 10), rep("b", 3))
ref = c(rep("b", 4), rep("a", 9))
relabel_class(class, ref)
relabel_class(class, ref, return_map = FALSE)

[Package cola version 1.0.1 Index]