Giter VIP home page Giter VIP logo

pa2-clarifying_instructions's Introduction

PA2-clarifying_instructions

Assignment 2: Step by step to understand the instructions

Hi everyone, Assignment 2 can be very challenging to be understood. The solution is easier than what it looks, and it's strongly connected with the example given in the instructions. However, I think it's worth spending some time to understand more about the logic behind the functions given in the instruction.

  1. makeVector is a function that stores a list of functions. I had no idea it was possible. Here is an easier example of a function that stores functions:

    ##function plusFunctions stores two functions:

    plustwo() : sums 2 to the given value

    plusthree() : sums 3 to the given value

    plusFunctions <- function (){ plustwo <- function(y) { x <- y + 2 return(x) } plusthree <- function(y) { x <- y + 3 return(x) } #the following line stores the two functions: list(plustwo = plustwo, plusthree = plusthree) }

  2. to use the functions stored in the main function, you need to subset the main function. To do this, you need the name of the main function + "$" + the name of the second function + (arguments)

    Example from the function above:

    a <- plusFunctions() a$plustwo(5) [1] 7 a$plusthree(5) [1] 8

  3. makeVector contains 4 functions: set, get, setmean, getmean. Let's start from get, because it's the easiest.

    get <- function() x

get is a function that returns the vector x stored in the main function. Doesn't require any input.

Example:

> a <- makeVector(c(5,1,3))
> a$get()
[1] 5 1 3
  1. set is a function that changes the vector stored in the main function.

    set <- function(y) { x <<- y m <<- NULL }

We don't need to use this function unless we want to change the vector. "x <<- y" substitutes the vector x with y (the input) in the main function (makeVector). If it was "x <- y" it would have substitute the vector x with y only in the set function. "m <<- NULL" restores to null the value of the mean m, because the old mean of the old vector is not needed anymore. The new mean needs to be recalculated through the function cachemean.

Example:

> a <- makeVector(c(5,1,3))
> a$get()
[1] 5 1 3
> a$set(c(7,4,1,2))
>a$get()
[1] 7 4 1 2
  1. setmean and getmean are functions very similar to set and get. They don't calculate the mean, they simply store the value of the input in a variable m into the main function makeVector (setmean) and return it (getmean).

    setmean <- function(mean) m <<- mean getmean <- function() m

This value "mean", input of seatmean, is supposed to be the mean of the vector x. However it simply stores a value, like in the following example:

> a<- makeVector(c(1,2,3,4))
> a$setmean(10)
> a$getmean()
[1] 10

As you see 10 is not the mean of vector (1,2,3,4), but because we stored it with setmean, we got it back with getmean.

  1. To store the 4 functions in the function makeVector, we need the function list(), so that when we assign makeVector to an object, the object has all the 4 functions.

     list(set = set, get = get,
          setmean = setmean,
          getmean = getmean)
    
  2. Now that makeVector is a bit more clear, let's observe the second function, cachemean. Input of cachemean is the object where makeVector is stored.

Example:

> a <- makeVector(c(1,2,3,4))
> cachemean (a)
  1. The first thing cachemean does is to verify the value m, stored previously with getmean, exists and is not NULL. If it exists in memory, it simply returns a message and the value m, that is supposed to be the mean, but not necessarily.

    m <- x$getmean() if(!is.null(m)) { message("getting cached data") return(m) }

  2. If it was the case, "return(m)" would have ended the function. So everything that follows this if() is a sort of else {}. data gets the vector stored with makeVector, m calculates the mean of the vector and x$setmean(m) stores it in the object generated assigned with makeVector.

    data <- x$get() m <- mean(data, ...) x$setmean(m) m

Hope this helps!

pa2-clarifying_instructions's People

Contributors

danielep avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pa2-clarifying_instructions's Issues

Nice, helpful explanation, but some inaccuracies

  1. Under (3), and elsewhere, you say things like this:

"get is a function that returns the vector x stored in the main function."

That's not accurate. The vector x WAS stored as a temporary local variable during the call to makeVector used e.g. to create your object named "a". Once that call to makeVector returned, the vector x persisted only in the environment associated with each of the functions stored as part of object a, e.g. a$get. You can see this with e.g. ls(environment(a$get)). The "main function" object itself, named here makeVector, does not have any persistent value for x. It would be more accurate to say that "a$get is a function that returns the vector x stored in its own environment.

  1. Under (7), you say:

"Input of cachemean is the object where makeVector is stored."

That also is inaccurate. The input of cachemean is an object where the value returned by a call to function makeVector is stored. The object where makeVector is stored is named "makeVector", and contains only the makeVector function definition.

Calling a sub function

Struggling with this programing. Calling a sub function is not working as mentioned below

plusFunctions <- function (){

  • plustwo <- function(y)
  • { x <- y + 2
  • return(x)
  • }
  • plusthree <- function(y)
  • { x <- y + 3
  • return(x)
  • }
  • }

a <- plusFunctions() a$plustwo(5)
Error: unexpected symbol in "a <- plusFunctions() a"
a <- plusFunctions() $plustwo(5)
Error in plusFunctions()$plustwo :
object of type 'closure' is not subsettable

plusFunctions() a$plustwo(5)

makeVector() and cachemean() missing before starting with item 3.

Hi Daniele,

In preparation of the upcoming R Programming course, I followed your tutorial step-by-step by creating my own Rmd. However, I bumped into a couple of issues (such as missing the discussed functions). But finally was able to solve them (and in the end I gained more insights!). However, I can imagine that other newbies might find it a bit too challenging to completely follow your tutorial, so I thought that maybe the PA2.Rmd may contain some enhancements you might find useful as well.

Anyway, thx again for your tutorial.

Cheers!
lars

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.