From e823b3a58b4beeb74d0087df7040e8f75c4fafab Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Wed, 18 Mar 2020 11:08:40 +0100 Subject: [PATCH] Implemented sortrows from base Matlab --- R/sortrows.R | 15 +++++++++++++++ man/sortrows.Rd | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 R/sortrows.R create mode 100644 man/sortrows.Rd diff --git a/R/sortrows.R b/R/sortrows.R new file mode 100644 index 0000000..28c4d25 --- /dev/null +++ b/R/sortrows.R @@ -0,0 +1,15 @@ +#' @title Sort rows of matrix or table +#' @description Emulates the behavior of the `sortrows` function on Matlab +#' @param A matrix +#' @param column ordering column +sortrows <- function(A, column = 1) { + if (length(column) == 1) { + new_row_order <- order(A[, column]) + } else if (length(column) == 2) { + new_row_order <- order(A[, column[1]], A[, column[2]]) + } else { + stop("Not yet implemented for 2+ tie-breakers") + } + A_reordered <- A[new_row_order, ] + return(A_reordered) +} \ No newline at end of file diff --git a/man/sortrows.Rd b/man/sortrows.Rd new file mode 100644 index 0000000..b761bec --- /dev/null +++ b/man/sortrows.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sortRows.R +\name{sortrows} +\alias{sortrows} +\title{Sort rows of matrix or table} +\usage{ +sortrows(A, column = 1) +} +\arguments{ +\item{A}{matrix} + +\item{column}{ordering column} +} +\description{ +Emulates the behavior of the `sortrows` function on Matlab +}