sort_matrix_rows: Sort matrix or data frame by a column or row names

View source: R/CodeAndRoll2.R

sort_matrix_rowsR Documentation

Sort matrix or data frame by a column or row names

Description

Sorts a numeric matrix or data frame by a specified column or by row names. The function can only handle sorting by a single column. It offers options to sort in increasing or decreasing order, and to control the placement of NA values.

Usage

sort_matrix_rows(
  df,
  column = NULL,
  rownames = FALSE,
  decrease = FALSE,
  na_last = TRUE
)

Arguments

df

A numeric matrix or data frame to be sorted. Default: none.

column

A column name or index by which to sort. The function can handle only single-column sorting. Default: none.

rownames

Logical. If TRUE, row names will be used for sorting instead of a column. Default: FALSE.

decrease

Logical. If TRUE, the data will be sorted in decreasing order. Default: FALSE.

na_last

Logical. If TRUE, NA values will be placed at the end. Default: TRUE.

Value

A sorted version of the input matrix or data frame.

Examples

df <- data.frame(AA = c(1, 2, 3), BB = c(3, 2, 1), row.names = letters[7:5])
sort_matrix_rows(df, "AA")
sort_matrix_rows(df, "AA", decrease = TRUE)
sort_matrix_rows(df, "A", rownames = TRUE)


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