Implemented sortrows from base Matlab

This commit is contained in:
Waldir Leoncio 2020-03-18 11:08:40 +01:00
parent 9770c512c4
commit e823b3a58b
2 changed files with 31 additions and 0 deletions

15
R/sortrows.R Normal file
View file

@ -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)
}

16
man/sortrows.Rd Normal file
View file

@ -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
}