Skip to content

Instantly share code, notes, and snippets.

@ashenkin
Last active June 12, 2017 10:57
Show Gist options
  • Save ashenkin/3f666de270dcb39d7a44dd9d1f76e5be to your computer and use it in GitHub Desktop.
Save ashenkin/3f666de270dcb39d7a44dd9d1f76e5be to your computer and use it in GitHub Desktop.
A function to reverse the process of scale()'ing a vector in R
unscale <- function(scaled, scale, center) {   
# provide either scale & center, or a scaled vector with the proper attributes   
if (missing(scale) | missing(center)) {       
stopifnot( c("scaled:center", "scaled:scale") %in% names(attributes(scaled)) )       
scale = attr(scaled, "scaled:scale")       
center = attr(scaled, "scaled:center")       
attr(scaled, "scaled:scale") <- NULL       
attr(scaled, "scaled:center") <- NULL   
}   
unscaled = scaled * scale + center   
return(unscaled)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment