get_max_colname_per_row: Get the Column Name corresponding to the Maximum Value in...

View source: R/CodeAndRoll2.R

get_max_colname_per_rowR Documentation

Get the Column Name corresponding to the Maximum Value in each Row (handles ambiguous matches)

Description

This function takes a numeric matrix as input and returns a named vector where each element corresponds to a row of the matrix. The names of the vector are the row names of the matrix, and the values are the column names where the maximum value of each row is found. If there are multiple columns with the maximum value in a row, the value for that row will be set to multi_max_str. If na.remove is set to TRUE, NA values will be removed before finding the maximum value.

Usage

get_max_colname_per_row(
  mat,
  na.remove = TRUE,
  collapse = "-",
  verbose = TRUE,
  multi_max_str = "multiple.maxima",
  suffix = "rows have multiple maxima."
)

Arguments

mat

A numeric matrix

na.remove

Logical. Should NA values be removed before finding the maximum value? Default: TRUE

collapse

Character. The character to use to collapse multiple column names into a single string. Default: "-"

verbose

Logical. Should messages be printed to the console? Default: TRUE

multi_max_str

Character. The string to use when multiple columns have the maximum value. Default: "multiple.maxima"

suffix

Character. The suffix to add to the multi_max_str string. Default: "rows have

Examples

mat <- matrix(data = c(1, 2, 3, 9, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)
colnames(mat) <- c("UVI1", "UVI2", "UVI3")
rownames(mat) <- c("Cell1", "Cell2")
mat
get_max_colname_per_row(mat)
mat[5] <- NA
mat[2] <- NaN
mat[1] <- 2
mat
get_max_colname_per_row(mat)


vertesy/CodeAndRoll2 documentation built on Nov. 20, 2024, 5:23 a.m.