Created
May 15, 2013 18:43
-
-
Save kevinushey/5586251 to your computer and use it in GitHub Desktop.
A handy function for swapping values in a vector with other values.
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
swap <- function(vec, from, to) { | |
tmp <- to[ match(vec, from) ] | |
tmp[is.na(tmp)] <- vec[is.na(tmp)] | |
return(tmp) | |
} | |
## example | |
swap( 1:10, c(5, 7), c(50, 70) ) ## exchange occurrences of 5 w/ 50, 7 w/ 70 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment