Skip to content

Instantly share code, notes, and snippets.

@thoolihan
Last active October 10, 2020 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoolihan/f656ccb37c551807e063ab2538fb4aeb to your computer and use it in GitHub Desktop.
Save thoolihan/f656ccb37c551807e063ab2538fb4aeb to your computer and use it in GitHub Desktop.
data <- -10:10
lims <- range(data)
x <- sample(data, 50, replace = TRUE)
par(mfrow=c(1, 5))
plot(x, ylim=lims, main="initial data")
# only positive
plot(pmax(x, 0), ylim=lims, main="clip bottom")
# only negative
plot(pmin(x, 0), ylim=lims, main="clip top")
# clip 1-3
plot(pmax(pmin(x,3), 1), ylim=lims, main="nested pmax & pmin")
clip <- function(x, lower, upper) {
pmax(pmin(x, upper), lower)
}
plot(clip(x, 1, 3), ylim=lims, main="clip function")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment