Caching the Mean of a Vector in R

Caching the mean of a Vector in R

m <- NULL begins by setting the mean to NULL as a placeholder for a future value

set <- function(y) {x <<- y; m <<- NULL} defines a function to set the vector, x, to a new
vector, y, and resets the mean, m, to NULL

get <- function() x returns the vector, x

setmean <- function(mean) m <<- mean sets the mean, m, to mean

getmean <- function() m returns the mean, m

list(set = set, get = get,setmean = setmean,getmean = getmean) returns the 'special
vector' containing all of the functions just defined

Caching the Inverse of a Matrix

Given the code in the OP, x$getinverse() should return NULL because one must execute cacheSolve() in order to populate the cache. I explain the details of how the sample code for this assignment works, including the need for the second function to populate the cache, in the stackoverflow answer Caching the Mean of a Vector.

That said, the program has three defects that prevent it from operating correctly.

  1. In cacheSolve(), m<-x$getinverse sets the value of m to a function, not the result of executing the getinverse() function

  2. In cacheSolve(), data<-x$get returns the address of the function get() instead of its contents.

  3. In cacheSolve(), x$setinverse(m) fails because the function setinverse() in makeCacheMatrix does not include an input argument.

Note that since I am a Community Mentor for the Hopkins R Programming course, I'm not allowed to post a complete solution because it would violate the Coursera Honor Code.

Once the errors are corrected, the code works like this:

> x <-makeCacheMatrix(matrix(c(1,0,0,0,1,0,0,0,2),ncol=3,nrow=3))
> cacheSolve(x)
[,1] [,2] [,3]
[1,] 1 0 0.0
[2,] 0 1 0.0
[3,] 0 0 0.5
> x$get()
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 2
> x$getinverse()
[,1] [,2] [,3]
[1,] 1 0 0.0
[2,] 0 1 0.0
[3,] 0 0 0.5
>

Getting attempt to apply non-function error in R

The error is generated by passing d = as.data.frame(d) to your function cacheSolve(d)
which then calls m <- x$getInverse() and generates the error because x is passed in as a data.frame, which does not have the getInverse() function defined.

The fix as follows enables the function to run by first creating the makeCacheMatrix() you've defined and setting its value with the $set() call, passing it the data.frame d

d = matrix(1:1000000, 5000, 200)
d = as.data.frame(d)
cm = makeCacheMatrix(d)
cm$set(d)
cacheSolve(cm)

R using references instead of copying data (and deleting the source if not used early)

Welcome to the wacky world of lazy evaluation and promises...

When you do:

cv<-makeVector(v)

your code never evaluates v, it just defines some functions in the object that are going to use v (now called x as the argument name) later.

So then you rm(v), and call cv$get(). Only at this point does x get looked at, and oh dear, v has gone.

The first time this hits you, it looks like a Heisenbug. If you put print(x) after the function(x){ line, then x prints out okay, and the function works. Get rid of the print(x) and the function fails again. Observing the system seems to change the state of the system, just like Quantum Theory.

The explanation is that printing x evaluates x, and so now x is no longer an unevaluated promise related to v. You can safely remove v.

The conventional way to work round this is to do something that evaluates x in the outer function context. In the absence of any other way, use force(x), which is a null-op function that evaluates its args.

In your third code fragment, calling cv$get() and then removing v works because cv$get() evaluates x, finds v, and all is well. x is now evaluated.

That explains your first problem, I'm stopping there because I suspect everything else is related...

tl;dr: just stick force(x) after your function definition

further reading: I suspect Hadley Wickham's Advanced R book explains this better.

R error, $ operator is invalid for atomic vectors

In your instruction you pass x as atomic vector. That is why $ does not work. Use it like this: cachemean(makeVector(1:6)) and you get a result.

It won't be cached, though, because this code creates a temporary variable that will get lost. You need a place to store it to. Look at this:

test <- makeVector(c(2,4,6,8))
test$getmean()
# returns NULL because R discards the object when the function has finished
cachemean(test)
test$getmean()
# returns 5


Related Topics



Leave a reply



Submit