Skip to content

Instantly share code, notes, and snippets.

@robertzk
Created December 6, 2013 00:36
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 robertzk/7816764 to your computer and use it in GitHub Desktop.
Save robertzk/7816764 to your computer and use it in GitHub Desktop.
R exercise: Create a replacement function that modifies a random location in vector. http://adv-r.had.co.nz/Functions.html
"random<-" <- function(object, position, value) {
object[sample(seq_along(object), 1)] <- value
object
}
# Example:
# > x <- c(1,2,3,4,5)
# > random(x) <- 10
# > x
# [1] 1 10 3 4 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment