Created
April 19, 2016 04:57
-
-
Save Ram-N/5575e29e55305da2ea42a93b6b04741d to your computer and use it in GitHub Desktop.
R multiple Grid substituitions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mgsub <- function(pattern, replacement, x) { | |
if (length(pattern)!=length(replacement)) { | |
stop("pattern and replacement do not have the same length.") | |
} | |
result <- x | |
for (i in 1:length(pattern)) { | |
result <- gsub(pattern[i], replacement[i], result) | |
} | |
result | |
} | |
domain_dict <- c('Management' = 'mgmt', 'University' = 'Univ', | |
'System' = 'sys') | |
#Usage similar to gsub | |
df$b <- mgsub(names(domain_dict), domain_dict, df$a) | |
df <- data.frame(a=c("Management asdf", "asdf University")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment