gsubOrdered: Global substitution into ordered factor

gsubOrderedR Documentation

Global substitution into ordered factor

Description

Global substitution into ordered factor

Usage

gsubOrdered(
  pattern,
  replacement,
  x,
  ignore.case = FALSE,
  perl = FALSE,
  fixed = FALSE,
  useBytes = FALSE,
  sortFunc = mixedSort,
  ...
)

Arguments

pattern, replacement, x, ignore.case, perl, fixed, useBytes

arguments sent to base::gsub()

sortFunc

function used to sort factor levels, which is not performed if the input x is a factor.

...

additional arguments are passed to sortFunc

Details

This function is an extension of base::gsub() that returns an ordered factor output. When input is also a factor, the output factor levels are retained in the same order, after applying the string substitution.

This function is very useful when making changes via base::gsub() to a factor with ordered levels, because it retains the the order of levels after modification.

Tips:

  • To convert a character vector to a factor, whose levels are sorted, use sortFunc=sort.

  • To convert a character vector to a factor, whose levels are the order they appear in the input x, use sortFunc=c.

  • To convert a character vector to a factor, whose levels are sorted alphanumerically, use sortFunc=mixedSort.

Value

factor whose levels are based upon the order of input levels when the input x is a factor; or if the input x is not a factor, it is converted to a factor using the provided sort function sortFunc.

See Also

Other jam string functions: asSize(), breaksByVector(), cPasteSU(), cPasteS(), cPasteUnique(), cPasteU(), cPaste(), fillBlanks(), formatInt(), gsubs(), makeNames(), mixedOrder(), mixedSortDF(), mixedSorts(), mixedSort(), mmixedOrder(), nameVectorN(), nameVector(), padInteger(), padString(), pasteByRowOrdered(), pasteByRow(), sizeAsNum(), tcount(), ucfirst(), uniques()

Examples

x <- c(paste0(
   rep(c("first", "second", "third"), 2),
   rep(c("Section", "Choice"), each=3)),
   "Choice");
f <- factor(x, levels=x);
f;

# default gsub() will return a character vector
gsub("(first|second|third)", "", f)
# converting to factor resets the factor level order
factor(gsub("(first|second|third)", "", f))

## gsubOrdered() maintains the factor level order
gsubOrdered("(first|third)", "", f)
gsubOrdered("(first)", "", f)

# to convert character vector to factor, levels in order they appear
gsubOrdered("", "", x, sortFunc=c)

# to convert character vector to factor, levels alphanumeric sorted
gsubOrdered("", "", x, sortFunc=mixedSort)


jmw86069/jamba documentation built on Oct. 9, 2024, 10:52 a.m.