Created
June 14, 2020 20:26
-
-
Save mrdwab/30a0eeb0eb795a746d59bf116a24a8ed to your computer and use it in GitHub Desktop.
This file contains 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
myFun <- function(vec, find, replace) { | |
if (length(find) != length(replace)) stop("incompatible find/replace") | |
if (all(find %in% vec)) { | |
pos <- which(vec == find[1]) | |
for (i in seq_along(pos)) { | |
ind <- pos[i]:(pos[i]+length(find)-1) | |
if (identical(vec[ind], find)) vec[ind] <- replace | |
} | |
} else { | |
message("nothing changed") | |
} | |
vec | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note the difference with
find_subvec
(https://stackoverflow.com/q/62377018/1270695).