Caching the mean of a Vector in R (list)

喜欢而已 提交于 2019-12-25 07:57:46

问题


This is a follow up to Caching the mean of a Vector in R and is specifically about the last line of code

list(set = set, get = get, setmean = setmean, getmean = getmean)

I am not understanding what the list is doing. The answer given at the linked question:

list() returns the 'special vector' containing all of the functions just defined

does not make much sense to me. I think that makeVector should be returning an object which has the approriate set and get methods, but now sure how this list() is doing that. What is the set on the left-hand side and what is the set on the right-hand side?

 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)
 }

回答1:


> z<- makeVector()
> z

the above statement will return a list of all the four functions in "makeVector " function,i.e., $set, $get, $setmean, $getmean

$set
function (y) 
{
    x <<- y
    m <<- NULL
}
<environment: 0x0000000008935dc8>

$get
function () 
x
<environment: 0x0000000008935dc8>

$setmean
function (mean) 
m <<- mean
<environment: 0x0000000008935dc8>

$getmean
function () 
m
<environment: 0x0000000008935dc8>

now you can access each function as a list object as shown below:

> z$set(c(1:20))
> z$get()
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
> z$setmean(mean(z$get()))
> z$getmean()
[1] 10.5

I hope that helps




回答2:


I provide an extensive walkthrough of the two functions in the second Johns Hopkins R Programming Assignment in my article Demystifying makeVector().

That said, here is the answer to how the list() statement works in makeVector().

Step 3: Create a new object by returning a list()

Here is the other part of the "magic" in the operations of the makeVector() function. The last section of code assigns each of these functions as an element within a list(), and returns it to the parent environment.

list(set = set, get = get,
     setmean = setmean,
     getmean = getmean)

When the function ends, it returns a fully formed object of type makeVector() to be used by downstream R code. One other important subtlety about this code is that each element in the list is named. That is, each element in the list is created with a elementName = value syntax, as follows:

    list(set = set,          # gives the name 'set' to the set() function defined above
         get = get,          # gives the name 'get' to the get() function defined above
         setmean = setmean,  # gives the name 'setmean' to the setmean() function defined above
         getmean = getmean)  # gives the name 'getmean' to the getmean() function defined above

Naming the list elements is what allows us to use the $ form of the extract operator to access the functions by name rather than using the [[ form of the extract operator, as in myVector[[2]](), to get the contents of the vector.

Here it's important to note that the cachemean() function REQUIRES an input argument of type makeVector(). If one passes a regular vector to the function, as in

 aResult <- cachemean(1:15)

the function call will fail with an error explaining that cachemean() was unable to access $getmean() on the input argument because $ does not work with atomic vectors. This is accurate, because a primitive vector is not a list, nor does it contain a $getmean() function, as illustrated below.

> aVector <- 1:10
> cachemean(aVector)
Error in x$getmean : $ operator is invalid for atomic vectors


来源:https://stackoverflow.com/questions/41918861/caching-the-mean-of-a-vector-in-r-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!