R Regular Expression: Replace entire string list with more specific information
Question
If I have data with a categorical variable that has elements such as "Q7", "Q2"
, etc., how do I replace it with "7 Queen", "2 Queen"
, etc.?
Solution
For starters, you'll get the most out of asking a question if you also include the code for what you've already tried. That said...
Perhaps not the most efficient way to solve this problem for large data sets, but these two methods seem to work just fine on your example. example(strsplit)
has a nice string-reversal function that we can put to use.
> strReverse <- function(x)
sapply(lapply(strsplit(x, NULL), rev), paste, collapse = "")
> strg <- c("Q7", "Q2")
> GS <- gsub("Q", strReverse(" Queen"), strg)
> strReverse(GS)
[1] "7 Queen" "2 Queen"
Another way would be to use paste
> S <- paste0(strReverse(strg), "ueen")
> gsub("Q", " Q", S)
[1] "7 Queen" "2 Queen"
OTHER TIPS
Regex
(Q)(\d)
Replace Regex
$2 Queen
Description
1st Capturing group (Q)
Q matches the character Q literally (case insensitive)
2nd Capturing group (\d)
\d match a digit [0-9]
g modifier: global. All matches (don't return on first match)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Note: You'd need one of these for each letter
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow