Samuel Colon
2015-Feb-19 20:13 UTC
[R] Issue writing correct code for Coursera assignment--performing matrix inversion
Hello All, I'm new to this mailing list, so please let me know if I've committed any posting faux-pas. I'm working on an assignment for my Coursera course; please see my code below in which I have tried to write two functions--to perform the task of matrix inversion and then caching that data. My code is below: makeCacheMatrix <- function(x = matrix()) { i <- NULL set <- function(y) { x <<- y i <<- NULL } get <- function() x setinverse <- function(solve) i <<- solve getinverse <- function() i list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) } cacheSolve <- function(x, ...) { i <- x$getinverse() if(!is.null(i)) { message("getting cached data") return(i) } data <- i$get() i <- solve(data, ...) x$setinverse(i) i } After I create a new matrix, x, and try to run cacheSolve(x), I receive this error: "attempt to apply non-function." I'm new to using the <<- operator, so I'm not sure if that's where my error is? Can anyone suggest how I can go about debugging the above code? Thanks very much, Sam Colon Coursera Student [[alternative HTML version deleted]]
Duncan Murdoch
2015-Feb-19 22:06 UTC
[R] Issue writing correct code for Coursera assignment--performing matrix inversion
On 19/02/2015 3:13 PM, Samuel Colon wrote:> Hello All, > > I'm new to this mailing list, so please let me know if I've committed any > posting faux-pas.Yes, you should probably be using the Coursera support resources instead of this list. Duncan Murdoch> > I'm working on an assignment for my Coursera course; please see my code > below in which I have tried to write two functions--to perform the task of > matrix inversion and then caching that data. My code is below: > > makeCacheMatrix <- function(x = matrix()) { > > i <- NULL > > set <- function(y) { > > x <<- y > > i <<- NULL > > } > > get <- function() x > > setinverse <- function(solve) i <<- solve > > getinverse <- function() i > > list(set = set, get = get, setinverse = setinverse, > > getinverse = getinverse) > > } > > > > cacheSolve <- function(x, ...) { > > i <- x$getinverse() > > if(!is.null(i)) { > > message("getting cached data") > > return(i) > > } > > data <- i$get() > > i <- solve(data, ...) > > x$setinverse(i) > > i > > } > > After I create a new matrix, x, and try to run cacheSolve(x), I receive > this error: "attempt to apply non-function." > > I'm new to using the <<- operator, so I'm not sure if that's where my error > is? > > Can anyone suggest how I can go about debugging the above code? > > Thanks very much, > > Sam Colon > Coursera Student > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >