orderMatrToRef: Order Lines of Matrix According to Reference (Character)...

View source: R/orderMatrToRef.R

orderMatrToRefR Documentation

Order Lines of Matrix According to Reference (Character) Vector

Description

This function orders lines of matrix mat according to a (character) reference vector ref. To do so, all columns of mat will be considered to use the first column from left with the best (partial) matching results. This function first looks for unambiguous perfect matches, and if not found successive rounds of more elaborte partial matching will be engaged: In case of no perfect matches found, grep of ref on all columns of mat and/or grep of all columns of mat on ref (ie 'reverse grep') will be applied (finally a 'two way grep' approach). Until a perfect match is found each element of ref will be tested on mat and inversely (for each column) each element of mat will be tested on ref. The approach with the best number of (unique) matches will be chosen. In case of one-to-many matches, it will be tried to use most complete lines (see also last example).

Usage

orderMatrToRef(
  mat,
  ref,
  addRef = TRUE,
  listReturn = TRUE,
  silent = FALSE,
  debug = FALSE,
  callFrom = NULL
)

Arguments

mat

(matrix, data.frame) main input of which rows should get re-ordered according to a (character) reference vector ref

ref

(character) reference imposing new order

addRef

(logical) add ref to output as new column

listReturn

(logical) allows retrieving more information in form of list

silent

(logical) suppress messages

debug

(logical) display additional messages for debugging

callFrom

(character) allow easier tracking of messages produced

Value

This function returns, depending on listReturn, either the input-matrix in new order or a list with $mat (the input matrix in new order), $grep (matched matrix) and $col indicating the colum of mat finally used

See Also

for basic ordering see match; checkGrpOrder for testing each line for expected order, checkStrictOrder to check for strict (ascending or descending) order

Examples

mat1 <- matrix(paste0("__",letters[rep(c(1,1,2,2,3),3) +rep(0:2,each=5)], rep(1:5)), ncol=3)
orderMatrToRef(mat1, paste0(letters[c(3,4,5,3,4)],c(1,3,5,2,4)))

mat2 <- matrix(paste0("__",letters[rep(c(1,1,2,2,3),3) +rep(0:2,each=5)], 
  c(rep(1:5,2),1,1,3:5 )), ncol=3)
orderMatrToRef(mat2, paste0(letters[c(3,4,5,3,4)],c(1,3,5,1,4)))

mat3 <- matrix(paste0(letters[rep(c(1,1,2,2,3),3) +rep(0:2,each=5)], 
  c(rep(1:5,2),1,1,3,3,5 )), ncol=3)
orderMatrToRef(mat3, paste0("__",letters[c(3,4,5,3,4)],c(1,3,5,1,3)))


wrMisc documentation built on Sept. 11, 2024, 6:10 p.m.