Skip to content

Instantly share code, notes, and snippets.

@nharrell04
Created June 2, 2016 02:22
Show Gist options
  • Save nharrell04/3b3582cdf359657daf121da0da0fcc59 to your computer and use it in GitHub Desktop.
Save nharrell04/3b3582cdf359657daf121da0da0fcc59 to your computer and use it in GitHub Desktop.
makeVector <- function(x = numeric()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setmean <- function(mean) m <<- mean
getmean <- function() m
list(set = set, get = get,
setmean = setmean,
getmean = getmean)
}
cachemean <- function(x, ...) {
m <- x$getmean()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- mean(data, ...)
x$setmean(m)
m
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment