Description Usage Arguments Details Value Author(s) See Also
Recode values of variable/factor matching patterns
1 2 |
x |
The character vector/factor to recode. |
patterns |
List of regular expressions for matching. |
targets |
Values to recode corresponding matches in 'patterns' into. |
default |
Default target value for those values of x that don't match any pattern. When default=NULL, non-matching values of x will be kept in the output. |
keep.na |
If TRUE then NA's in x will be retained in the output. |
ignore.case |
If TRUE (default) then matching is not case sensitive. |
ignore.punc |
If TRUE (default) then punctuation will be ignored when matching. |
... |
optional arguments to grepl (e.g. use fixed=TRUE for string instead of regexp matching) |
This function is like 'recodeVar' except that it uses regular expressions to match the source variables (using 'grepl'). For each unique value of 'x' the regexps in 'patterns' are tried in turn until a match is found, then the corresponding element of 'targets' is used to recode the value. If length(patterns) > length(targets) then the final target will be used for all excess patterns. The 'default' and 'keep.na' parameters are the same as for 'recodeVar'. To do string matching instead of regular expression matching use fixed=TRUE
A vector
Ben Veal
recode
for recoding numbers (in library(car)), recodeVar
, recodeAs
,
colwise2
for recoding multiple dataframe columns simultaneously (in library(plyr)).
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.