Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active June 13, 2023 09:01
Show Gist options
  • Save mGalarnyk/e7e24f837f52507c6dd585f9b53d7d9b to your computer and use it in GitHub Desktop.
Save mGalarnyk/e7e24f837f52507c6dd585f9b53d7d9b to your computer and use it in GitHub Desktop.
R Programming Quiz 2 (Week 2) John Hopkins Data Science Specialization Coursera for the github repo https://github.com/mGalarnyk/datasciencecoursera

R Programming Quiz 2

github repo for rest of specialization: Data Science Coursera

Question 1

Suppose I define the following function in R

cube <- function(x, n) {
        x^3
}

What is the result of running cube(3) in R after defining this function?

Answer

The number 27 is returned

Question 2

The following code will produce a warning in R.

x <- 1:10
if(x > 5) {
        x <- 0
}

Why?

Answer

'x' is a vector of length 10 and 'if' can only test a single logical statement.

Question 3

Consider the following function

f <- function(x) {
        g <- function(y) {
                y + z
        }
        z <- 4
        x + g(x)
}

If I then run in R

	z <- 10
	f(3)

What value is returned?

Answer

10

Question 4

Consider the following expression:

x <- 5
y <- if(x < 3) {
        NA
} else {
        10
}

What is the value of 'y' after evaluating this expression?

Answer

10

Question 5

Consider the following R function

h <- function(x, y = NULL, d = 3L) {
        z <- cbind(x, d)
        if(!is.null(y))
                z <- z + y
        else
                z <- z + f
        g <- x + y / z
        if(d == 3L)
                return(g)
        g <- g + 10
        g
}

Which symbol in the above function is a free variable?

Answer

f

Question 6

What is an environment in R?

Answer

a collection of symbol/value pairs

Question 7

The R language uses what type of scoping rule for resolving free variables?

Answer

lexical scoping

Question 8

How are free variables in R functions resolved?

Answer

The values of free variables are searched for in the environment in which the function was defined

Question 9

What is one of the consequences of the scoping rules used in R?

Answer

All objects must be stored in memory Correct 1.00

Question 10

In R, what is the parent frame?

Answer

It is the environment in which a function was called

@dhiaeddine-salhi
Copy link

install.packages("data.table")

library("data.table")

pollutantmean <- function(directory, pollutant, id = 1:332) {

Format number with fixed width and then append .csv to number

fileNames <- paste0(directory, '/', formatC(id, width=3, flag="0"), ".csv" )

Reading in all files and making a large data.table

lst <- lapply(fileNames, data.table::fread)
dt <- rbindlist(lst)

if (c(pollutant) %in% names(dt)){
return(dt[, lapply(.SD, mean, na.rm = TRUE), .SDcols = pollutant][[1]])
}
}

Example usage

pollutantmean(directory = '~/Desktop/specdata', pollutant = 'sulfate', id = 20)

@havilah97
Copy link

Can someone please explain Q3?

@hrysks082
Copy link

Explainations about Q3

f <- function(x = 3 ) {
g <- function(y = 3) {
y = 3 + z = 4 # y = 3, z = 4, result = 7 # g(3) = 7 as result
}
z=4 <- 4
x + g(x) # x = 3 and g(3) = 7, result = 3 + 7 = 10
}
z <- 10 # Not important because z change in the function to z <- 4
f(3)

@spongeshu
Copy link

spongeshu commented Sep 9, 2020

Explainations about Q3

f <- function(x = 3 ) {
g <- function(y = 3) {
y = 3 + z = 4 # y = 3, z = 4, result = 7 # g(3) = 7 as result
}
z=4 <- 4
x + g(x) # x = 3 and g(3) = 7, result = 3 + 7 = 10
}
z <- 10 # Not important because z change in the function to z <- 4
f(3)

Hi there, I know you're right on this question as I have run it in R, but could you elaborate on why g(3) =7 but not g=7 itself?

@hrysks082
Copy link

hrysks082 commented Sep 20, 2020

Hello

g(3) = 7
because y= 3 and z = 4 which give us y+z = 7

   g <- function(y = 3) {
            y + z # y = 3 and z = 4
    }
    z <- 4 # This the value of z because when you affect a value in the memory it keeps the last value. 
    g(3)

why g(3) =7 but not g=7 itself?
Simple to understand g it is the name of the function and when you write a function it can be emply or with a parameter(s) all depends how you defined your function, in our case the function has parameter g(3 ) = g <- function(3){ .... }. Now when you want to use the function you call it by the name plus the parameter(s). it is for that you can't only use g but you must use g(3)

@nauman0312
Copy link

Question 5, please. need explaination.

@aaronmorrisNC
Copy link

aaronmorrisNC commented Sep 9, 2022

This may be a very basic question, but regarding Question 3, why does Y=3? Would the single input of F(3) assign the value of 3 to both X and Y? Is there a mechanism with r that will allow one to see what values are being substituted for what variables in real-time, perhaps as the function is being executed?

@LesterYY
Copy link

The reason why y=3 is not because f(3) assign the value of 3 to both x and y at the same time, it's actually because the "g(x)" in the code "x+g(x)" assign value to y in g(y), we first use f(3) to assign the value of 3 to x in f(x), then the code "x+g(x)" will become "3+g(3)", and that's when the value of 3 is assigned to y. I hope this will help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment