3

I am getting this error:

Error in x$getinverse : $ operator is invalid for atomic vectors

My code is this. I can't understand where am I making a mistake.

##create a function which starts with a null matrix argument
makeCacheMatrix <- function(x = matrix()) { 
  ## initialize the value of the matrix inverse to NULL
  matrixinverse <- NULL                     
  ## delcare another function set where the value will be cached in 1. Matrix is created
  ## for the first time. 2. changes made to cached matrix
  set <- function(y) {                      
    x <<- y
    ## change the value of inverse of the matrix in case the matrix was changed.
    matrixinverse <<- NULL              
  }
  ## gets the value of the inverse
  get <- function() x                           
  #calculates the inverse of non-singular matrix via the solve function
  setinverse <- function(solve) matrixinverse <<- solve 
  # gets the inverse     
  getinverse <- function() matrixinverse        
  ## passes the value of the function makeCacheMatrix        
  list(set = set, get = get,                    
       setinverse = setinverse,
       getinverse = getinverse)
}

# used to get the cache of the matrix
cacheSolve<- function(x, ...) {                 
  matrixinverse <- x$getinverse()
  #if the inverse exists, it gets it.
  if(!is.null(matrixinverse)) {                 
    message("getting cached data - Inverse of the matrix")
    return(matrixinverse)
  }
  #if the inverse if not there, first it is calculated and then retrieved.
  data <- x$get()                               
  matrixinverse <- solve(data, ...)
  x$setinverse(matrixinverse)
  matrixinverse
}
vrajs5
  • 4,066
  • 1
  • 27
  • 44
nEO
  • 5,305
  • 3
  • 21
  • 25
  • Welcome to SO! Please take a look at [this guide](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and edit your question to make it reproducible. Please note that general debugging questions are off-topic for SO, so try to be specific. – tonytonov Jun 11 '14 at 07:41
  • `$` cannot evaluate an object. All it does is to look after a column name in `x` you've specified after `x$`. In other words, you can't attach a function after `$`. You probably should do something like `getinverse(x)` although I don't really understand how you defined that function – David Arenburg Jun 11 '14 at 07:52
  • 2
    The code works for me if I run `cacheSolve(makeCacheMatrix(2*diag(3)))` it returns the inverse as expected. What command are you running to cause the error? – Miff Jun 11 '14 at 09:40
  • @DavidArenburg: I am sorry, but what you are saying is incorrect. Try this example: `lapply(list(sum, mean, diff), function(x) x(1:10))`. – asb Jun 11 '14 at 10:03
  • 1
    @asb what your example has to do with `$`? – David Arenburg Jun 11 '14 at 10:05
  • Okay, `x = list(a=sum); x$a(1:10)`. In both cases the idea is the same. – asb Jun 11 '14 at 10:06
  • @asb, that's because `a` belong to object `x`, so it's call `a` and `a` evaluates `1:10` in his case `getinverse` does not belong to `x` as far as I can tell – David Arenburg Jun 11 '14 at 10:11
  • @DavidArenburg: I assumed that x being passed cacheSolve is output from makeCacheMatrix. Never mind. – asb Jun 11 '14 at 10:49
  • @asb, maybe it is. I'm lost in his syntax and also didn't test it because he didn't supply a data set. I just assumed by the error he's got that this is the issue – David Arenburg Jun 11 '14 at 10:52
  • You're coding in a rather odd and difficult manner. What's the goal of all this? – Carl Witthoft Jun 11 '14 at 11:31
  • Thanks guys. Found the error. Now its running fine. – nEO Jul 07 '14 at 06:40
  • It made my headache too, but what I was forgetting to do was creating an cache object, as the cacheSolve was calling it, and when it did not exist then x has been seen as a vector, hence the error message. Thanks @Miff for a clue in the code. – Dariusz Bielak Jun 22 '16 at 23:06

2 Answers2

6

You probably are passing the matrix itself to the cacheSolve function, rather than passing the return object of the makeCacheMatrix. I was facing the same problem earlier. I corrected the input parameter for cacheSolve and it worked.

Here is the illustration:

> mat<-matrix(c(1,4,9,0,-3,2,2,7,8),3,3)
> mat
     [,1] [,2] [,3]
[1,]    1    0    2
[2,]    4   -3    7
[3,]    9    2    8

m1<-makeCacheMatrix(mat)
> cacheSolve(mat)

Error in x$getinverse : $ operator is invalid for atomic vectors

But if you correct the input and re-run:

> cacheSolve(m1)
         [,1]    [,2]     [,3]
[1,] -1.18750  0.1250  0.18750
[2,]  0.96875 -0.3125  0.03125
[3,]  1.09375 -0.0625 -0.09375
Dharman
  • 30,962
  • 25
  • 85
  • 135
user2582651
  • 33
  • 1
  • 8
-1

I found changing the function to specify that y is also a matrix solved the problem. Hope this helps!

set <- function(y = matrix()) {                      
    x <<- y*
    ...
}
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51