Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active February 20, 2024 23:43
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 38 You must be signed in to fork a gist
  • Save mGalarnyk/972074f79957940eec765b216d526484 to your computer and use it in GitHub Desktop.
Save mGalarnyk/972074f79957940eec765b216d526484 to your computer and use it in GitHub Desktop.
R Programming Quiz 1 (Week 1) John Hopkins Data Science Specialization Coursera for the github repo https://github.com/mGalarnyk/datasciencecoursera

R Programming Quiz 1 (JHU) Coursera

github repo for rest of specialization: Data Science Coursera

Question 1

R was developed by statisticians working at...

Answer

The University of Auckland

Question 2

The definition of free software consists of four freedoms (freedoms 0 through 3). Which of the following is NOT one of the freedoms that are part of the definition?

Answer

The freedom to sell the software for any price.

Question 3

In R the following are all atomic data types EXCEPT

Answer

matrix

Question 4

If I execute the expression x <- 4 in R, what is the class of the object 'x' as determined by the `class()' function?

Answer

numeric

x <- 4
class(x)

Question 5

What is the class of the object defined by x <- c(4, TRUE)?

Answer

numeric

x <- c(4, TRUE)
class(x)

Question 6

If I have two vectors x <- c(1,3, 5) and y <- c(3, 2, 10), what is produced by the expression cbind(x, y)?

Answer

a 3 by 2 numeric matrix

x <- c(1,3, 5)
y <- c(3, 2, 10)
cbind(x, y)

Question 7

A key property of vectors in R is that

Answer

elements of a vector all must be of the same class

Question 8

Suppose I have a list defined as x <- list(2, "a", "b", TRUE). What does x[[1]] give me?

Answer

a numeric vector containing the element 2

x <- list(2, "a", "b", TRUE)
x[[1]]

class(x[[1]])

Question 9

Suppose I have a vector x <- 1:4 and a vector y <- 2. What is produced by the expression x + y?

Answer

a numeric vector with elements 3, 4, 5, 6.

x <- 1:4
y <- 2
x + y

class(x + y)

Question 10

Suppose I have a vector x <- c(17, 14, 4, 5, 13, 12, 10) and I want to set all elements of this vector that are greater than 10 to be equal to 4. What R code achieves this?

Answer

x[x >= 11] <- 4

x <- c(17, 14, 4, 5, 13, 12, 10)
x[x >= 11] <- 4
x

Question 11

In the dataset provided for this Quiz, what are the column names of the dataset?

Answer

Ozone, Solar.R, Wind, Temp, Month, Day

# install package if doesnt exist 
install.packages("data.table")
library("data.table")

# Reading in data
quiz_data <- fread('hw1_data.csv')

# Column names of the dataset 
names(quiz_data)

Question 12

Extract the first 2 rows of the data frame and print them to the console. What does the output look like?

Answer

  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
# First two rows 
quiz_data[c(1,2)]

Question 13

How many observations (i.e. rows) are in this data frame?

Answer

153

nrows(quiz_data)

Question 14

Extract the last 2 rows of the data frame and print them to the console. What does the output look like?

Answer

   Ozone Solar.R Wind Temp Month Day
152    18     131  8.0   76     9  29
153    20     223 11.5   68     9  30
tail(quiz_data, 2)

Question 15

What is the value of Ozone in the 47th row?

Answer

21

quiz_data[47, Ozone]

Question 16

How many missing values are in the Ozone column of this data frame?

Answer

37

# Going back to data.frame because dont it hasnt been taught yet in this specialization
hw1 = read.csv('hw1_data.csv')
sub = subset(quiz_data, is.na(Ozone))
nrow(sub)
# Can also remmove Missing Values using Something like This
quiz_data[complete.cases(quiz_data),]

Question 17

What is the mean of the Ozone column in this dataset? Exclude missing values (coded as NA) from this calculation.

Answer

42.1

Explanation

The 'mean' function can be used to calculate the mean.

hw1 = read.csv('hw1_data.csv')
sub = subset(hw1, !is.na(Ozone), select = Ozone)
apply(sub, 2, mean) 

Question 18

Extract the subset of rows of the data frame where Ozone values are above 31 and Temp values are above 90. What is the mean of Solar.R in this subset?

Answer

212.8

quiz_data = read.csv('hw1_data.csv')
sub = subset(quiz_data, Ozone > 31 & Temp > 90, select = Solar.R)
apply(sub, 2, mean)

Question 19

What is the mean of "Temp" when "Month" is equal to 6?

Answer

79.1

Explanation

quiz_data = read.csv('hw1_data.csv')
sub = subset(hw1, Month == 6, select = Temp)
apply(sub, 2, mean)

Question 20

What was the maximum ozone value in the month of May (i.e. Month = 5)?

Answer

115

Explantion

quiz_data = read.csv('hw1_data.csv')
sub = subset(quiz_data, Month == 5 & !is.na(Ozone), select = Ozone)
apply(sub, 2, max)
@Armbarbarian
Copy link

Thanks for this, so helpful. The course didn't go through a lot of the above code

@Munim2001
Copy link

Thank you so much for your help

@Rgill800
Copy link

Rgill800 commented May 7, 2020

Incredibly helpful

@lekanfaye
Copy link

Thanks for your help, the 2nd part is not covered in the lecture

@Arunan-R
Copy link

Thanks for the help. I went through the coursework twice but couldnt find how to do operations on the columns of the tables, This was very helpful!

@Wainaina16
Copy link

Thanks for the help

@stocke777
Copy link

coding part is right but not all

@elbobbyjose
Copy link

Thanks my friend!

@yashg008
Copy link

thank you so much

@omarabdelaz1z
Copy link

Very Helpful and kinda new commands for me, Thank you.

@Dizartx
Copy link

Dizartx commented Aug 17, 2020

Thank you!

@DrDoofenshmirz
Copy link

thank u so much .
some of the questions in the quiz require code that hasn't been taught yet in the course

@Willto884
Copy link

Very helpful... thank u very much...!

@mastep25
Copy link

Thank you very much for this help.
You really contributed to my pass mark.
Thanks

@Sakshamgoel
Copy link

A big help to all who don't wanna buy the course but still want to learn.

@jhw3
Copy link

jhw3 commented Jun 11, 2021

apply(sub, 2, max) what does the 2 represent in this?

@sarahguagliardo
Copy link

I'm not entirely sure how to use the "apply" function yet, but I was able to get the same answers using commands like >mean(sub$Temp) after creating the subset correctly. The course doesn't go over the correct format for subsetting data very well so this quiz was really hard!

@GarimaSodhi
Copy link

Thank you so much. This quiz was hard.

@nizburfat
Copy link

Rating, Cocoa.Percent, and Company.Location. You decide to use the select() function to create a new data frame with only these three variables.

Assume the first part of your code is:

trimmed_flavors_df <- flavors_df %>%

Add the code chunk that lets you select the three variables.

@nizburfat
Copy link

Assume the first part of your code is:

trimmed_flavors_df %>%

You want to use the summarize() and mean() functions to find the mean rating for your data. Add the code chunk that lets you find the mean value for the variable Rating.

@michaeladams01
Copy link

michaeladams01 commented Nov 17, 2021

This came in so clutch. Thank you sir.

@Krispeta
Copy link

Krispeta commented Feb 9, 2022

I was so confused, looking everywhere, going back on the book and lessons, until I had to look for this! thank you, you saved the day, I was going bananas!!

@bikrammajhi
Copy link

bikrammajhi commented Oct 12, 2022

Thank you for helping me out in my assignment. I initially panic since the question answer were not there in videos.

@Divyam6969
Copy link

thank youuu, this quiz was way tooo long lol

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