Skip to content

Instantly share code, notes, and snippets.

@kevinushey
Created May 15, 2013 18:43
Show Gist options
  • Save kevinushey/5586251 to your computer and use it in GitHub Desktop.
Save kevinushey/5586251 to your computer and use it in GitHub Desktop.
A handy function for swapping values in a vector with other values.
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