matchMatrixLinesToRef: Match All Lines Of Matrix To Reference Note

View source: R/matchMatrixLinesToRef.R

matchMatrixLinesToRefR Documentation

Match All Lines Of Matrix To Reference Note

Description

This function allows adjusting the order of lines of a matrix mat to a reference character-vector ref, even when initial direct matching of character-strings using match is not possible/successful. In this case, various variants of using grep will be used to see if unambiguous matching is possible of characteristic parts of the text. Besides, it will tested if common preceeding text ('words') or leading Os don't allow successful direct macthing.

Usage

matchMatrixLinesToRef(
  mat,
  ref,
  exclCol = NULL,
  addRef = TRUE,
  inclInfo = FALSE,
  silent = FALSE,
  debug = FALSE,
  callFrom = NULL
)

Arguments

mat

(matrix or data.frame) main input, all columns of mat will be tested for (partial) matching of ref

ref

(character, length must match ) reference for trying to match each of the columns of mat

exclCol

(character or integer) column-name or -index of column to ignore/exclude when looking for matches

addRef

(logical), if TRUE the content of ref will be added to mat as additional column

inclInfo

(logical) allows returning list with new matrix and additional information

silent

(logical) suppress messages

debug

(logical) additional messages for debugging

callFrom

(character) allows easier tracking of messages produced

Details

This function tests all columns of mat to find perfect matching results to the reference ref. In case no direct matching is possible, grep will be used to find the best partial matching. All columns of mat will be tested an the column giving the best resuts will be used. The order of the rows of input mat will be adjusted according to the matching results.

If addRef=TRUE, the reference will be included as additional column to the results, too.

Value

This function returns the input matrix in an adjusted order (plus an optional additional column showing the reference) or if inclInfo=TRUE a list with $mat (adjusted matrix), $byColumn, $newOrder and $method; the reference can bee added as additional last column if addRef=TRUE

See Also

match, grep, trimRedundText, replicateStructure

Examples

## Note : columns b and e allow non-ambigous match, not all elements of e are present in a
mat0 <- cbind(a=c("mvvk","axxd","bxxd","vv"),b=c("iwwy","iyyu","kvvh","gxx"), c=rep(9,4),
  d=c("hgf","hgf","vxc","nvnn"), e=c("_vv_","_ww_","_xx_","_yy_"))
matchMatrixLinesToRef(mat0[,1:4], ref=mat0[,5])
matchMatrixLinesToRef(mat0[,1:4], ref=mat0[1:3,5], inclInfo=TRUE)
matchMatrixLinesToRef(mat0[,-2], ref=substring(mat0[,2], 2, 3), inclInfo=TRUE)   

## reverse grep (grep ref in all columns of mat)
matchMatrixLinesToRef(mat0, ref=c("iyy","vv","iw","gx"), inclInfo=TRUE)   

## trimming of heading 0s
matchMatrixLinesToRef(matrix(11:18, ncol=2), ref=c("018","16","0015","017"))

wrMisc documentation built on Nov. 18, 2026, 5:06 p.m.