Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Created April 19, 2016 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ram-N/5575e29e55305da2ea42a93b6b04741d to your computer and use it in GitHub Desktop.
Save Ram-N/5575e29e55305da2ea42a93b6b04741d to your computer and use it in GitHub Desktop.
R multiple Grid substituitions.
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