Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active December 8, 2024 18:06
Show Gist options
  • Save mGalarnyk/7a56f1f5a70a92c23fb7e9911fc676d4 to your computer and use it in GitHub Desktop.
Save mGalarnyk/7a56f1f5a70a92c23fb7e9911fc676d4 to your computer and use it in GitHub Desktop.
R Programming Quiz 3 (Week 3) John Hopkins Data Science Specialization Coursera for the github repo https://github.com/mGalarnyk/datasciencecoursera

R Programming Quiz 3

github repo for rest of specialization: Data Science Coursera

Question 1

Take a look at the 'iris' dataset that comes with R. The data can be loaded with the code:

library(datasets)
data(iris)

A description of the dataset can be found by running

?iris

There will be an object called 'iris' in your workspace. In this dataset, what is the mean of 'Sepal.Length' for the species virginica? (Please only enter the numeric result and nothing else.)

Answer

6.588

# if you don't have data.table installed
# install.packages("data.table")

library(data.table)
iris_dt <- as.data.table(iris)
iris_dt[Species == "virginica",round(mean(Sepal.Length)) ]

Question 2

Continuing with the 'iris' dataset from the previous Question, what R code returns a vector of the means of the variables 'Sepal.Length', 'Sepal.Width', 'Petal.Length', and 'Petal.Width'?

Answer

apply(iris[, 1:4], 2, mean)

Question 3

Load the 'mtcars' dataset in R with the following code

library(datasets)
data(mtcars)

There will be an object names 'mtcars' in your workspace. You can find some information about the dataset by running

?mtcars

How can one calculate the average miles per gallon (mpg) by number of cylinders in the car (cyl)?

Answer

with(mtcars, tapply(mpg, cyl, mean))

Question 4

Continuing with the 'mtcars' dataset from the previous Question, what is the absolute difference between the average horsepower of 4-cylinder cars and the average horsepower of 8-cylinder cars?

Answer

126.5779

mtcars_dt <- as.data.table(mtcars)
mtcars_dt <- mtcars_dt[,  .(mean_cols = mean(hp)), by = cyl]
round(abs(mtcars_dt[cyl == 4, mean_cols] - mtcars_dt[cyl == 8, mean_cols]))

Question 5

If you run

debug(ls)

what happens when you next call the 'ls' function?

Answer

Execution of 'ls' will suspend at the beginning of the function and you will be in the browser.

@xuitex
Copy link

xuitex commented May 5, 2019

Answer 1 and 4 are not correct as shown in my case. Will you please review it.

@deepthi248
Copy link

Hi,

Can you please explain the following line of code which is used in the question 4.
*mtcars_dt <- mtcars_dt[, .(mean_cols = mean(hp)), by = cyl]**.

Thanks in advance

@H098
Copy link

H098 commented May 9, 2020

Answer 1 and 4 are not correct as shown in my case. Will you please review it.

write the number rounded
q1
the answer is 7
q4
the answer is 127

@Manika1105
Copy link

ques 1 answer is 7
ques 3 answers are
with(mtcars, tapply(mpg, cyl, mean))
tapply(mtcars$mpg,mtcars$cyl,mean)
sapply(split(mtcars$mpg,mtcars$cyl),mean)

ques 4 answer is 127

@NoofJaved
Copy link

tapply(mtcars$hp,mtcars$cyl=="8",mean)-tapply(mtcars$hp,mtcars$cyl=="4",mean)
=127

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