0

I am working on am R assignment of lexical looping and I am a newbie to R. I have two methods, my code is below. I have just given line numbers to make it easier for me to explain and for you to understand, they doesn't exist in real and anyway this method is not the problem as I believe.

makeCacheMatrix <- function(x = matrix()) {

    inv<- NULL

    set<-function(y){

        inv<<- NULL

        x<<-y

    }

    get<-function() x

    getinverse<-function() inv

    setinverse<-function(inversed_matrix) inv<-inversed_matrix

    list(setmatrix=set, getmatrix=get, getinverse = getinverse, setinverse = setinverse)

 }

This function works well and all the data gets populated as expected, I have tested it explicitly. Now I create an object from this function

my_matrix<-makeCacheMatrix(iris_subset)

Now the second function

cacheSolve <- function(cachedMatrix, ...) {

   inversed_matrix<-cachedMatrix$getinverse

   if(!is.null(inversed_matrix)){

       message("Getting cached inversed matrix")

       return(inversed_matrix)

   }

   inversed_matrix<-cachedMatrix$getmatrix

   cachedMatrix$setinverse(inversed_matrix)

   inversed_matrix

}

Now I execute following line of code, where "my_matrix" is the object of the function makeCacheMatrix i created above

m<-cacheSolve(my_matrix)

This is where I have problem. When I perform the null check at Line 3 it fails because it instead of returning the value, the code in Line 2 returns the actual code from the function "makeCacheMatrix". i.e instead of a NULL on the first run it returns the text "function() inv", which is actually the code written in the function makeCacheMatrix

Here's the data:

structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6), Sepal.Width = c(3.5, 
3, 3.2, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5), Petal.Width = c(0.2, 
0.2, 0.2, 0.2)), .Names = c("Sepal.Length", "Sepal.Width", "Petal.Length", 
"Petal.Width"), row.names = c(NA, 4L), class = "data.frame")
Hack-R
  • 22,422
  • 14
  • 75
  • 131

1 Answers1

0

I subset the data as you described in the comments:

iris <- iris[1:4,1:4]

The first step

my_matrix <- makeCacheMatrix(iris)

Produces

my_matrix
$setmatrix
function (y) 
{
    inv <<- NULL
    x <<- y
}
<environment: 0x0000000013442500>

$getmatrix
function () 
x
<environment: 0x0000000013442500>

$getinverse
function () 
inv
<environment: 0x0000000013442500>

$setinverse
function (inversed_matrix) 
inv <- inversed_matrix
<environment: 0x0000000013442500>

I'm not sure what you were trying to do there but, given the data structure produced in the first step, naturally

cachedMatrix$getinverse

evaluates to

function() inv
<environment: 0x0000000012f859d8>

So, the if statement will evaluate to TRUE and returns exactly that.

Thus the problem is in your first function. I can't follow your logic well enough to understand what you were trying to accomplish in that first function, but once you fix that then your 2nd function will return what it's supposed to.

If you need further help on the 1st function I'd suggest to review the R documentation on functions to try to understand how to write them, then if you run into a problem open another question and provide comments for each line to explain what you were trying to do and what result you expected.

Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • The assignment is about working with closures. I remember answering a question about the same assignment. So, this question is probably a duplicate. – Roland Oct 06 '16 at 06:06
  • Hi Hack, I am travelling right now, I shall share some observations once I a back on Monday. – Simrat Singh Oct 07 '16 at 00:36
  • Hi @Roland, is it possible for you to provide me the link to that question if it was done in recent past? – Simrat Singh Oct 07 '16 at 00:37
  • @Hack-R, I just rewrote the same program and its working, there is no difference at all between the code and still it works. Coming to the values in "my_matrix" in your comment above even in the working program "my_matrix" has the same content. I am really boggled on what the issue is – Simrat Singh Oct 11 '16 at 22:21
  • @SimratSingh Without seeing the code, I can't say. Add the working code to the question so that I can reproduce it and then we can figure it out. – Hack-R Oct 11 '16 at 22:23