gsubOrdered | R Documentation |
Global substitution into ordered factor
gsubOrdered(
pattern,
replacement,
x,
ignore.case = FALSE,
perl = FALSE,
fixed = FALSE,
useBytes = FALSE,
sortFunc = mixedSort,
...
)
pattern , replacement , x , ignore.case , perl , fixed , useBytes |
arguments sent to |
sortFunc |
function used to sort factor levels, which
is not performed if the input |
... |
additional arguments are passed to |
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
.
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
.
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()
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.