Hi John, Wouldn't you get the same with just mapply(sub, patt, repl, X) ?
Nael On Tue, Oct 7, 2008 at 9:58 PM, Thaden, John J <[EMAIL PROTECTED]> wrote: > R pattern-matching and replacement functions are > vectorized: they can operate on vectors of targets. > However, they can only use one pattern and replacement. > Here is code to apply a different pattern and replacement > for every target. My question: can it be done better? > > sub2 <- function(pattern, replacement, x) { > len <- length(x) > if (length(pattern) == 1) > pattern <- rep(pattern, len) > if (length(replacement) == 1) > replacement <- rep(replacement, len) > FUN <- function(i, ...) { > sub(pattern[i], replacement[i], x[i], fixed = TRUE) > } > idx <- 1:length(x) > sapply(idx, FUN) > } > > #Example > X <- c("ab", "cd", "ef") > patt <- c("b", "cd", "a") > repl <- c("B", "CD", "A") > sub2(patt, repl, X) > > -John > > Confidentiality Notice: This e-mail message, including a...{{dropped:8}} > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.