Course Outline
-
segmentLearnosity
-
segmentCKCode
-
ckcode-chapter-x1-exploring-with-R
list Items Test Book
Book
ckcode ⌲ chapter-x1-exploring-with-R
print("Hello world!")
print("Hello world!")
ex() %>%
check_function("print") %>%
check_arg("x") %>%
check_equal()
CK Code: X1_Code_Started_01
sum(100, 500, 23)
sum(100, 500, 23)
ex() %>%
check_function("sum") %>%
check_arg("...", arg_not_specified_msg = "Make sure you don't delete what's inside the parentheses.") %>%
check_equal(incorrect_msg = "Make sure you don't change what's inside the parentheses.")
CK Code: X1_Code_Started_02
# a few basic arithmetic things
5 + 1
10 - 3
2 * 4
9 / 3
# a few basic arithmetic things
5 + 1
10 - 3
2 * 4
9 / 3
ex() %>% {
check_operator(., "+") %>% check_result() %>% check_equal()
check_operator(., "-") %>% check_result() %>% check_equal()
check_operator(., "*") %>% check_result() %>% check_equal()
check_operator(., "/") %>% check_result() %>% check_equal()
}
CK Code: X1_Code_Started_03
require(coursekata)
# type whatever you want
# see... blah blah blah
# no solution, but need code to show submit button
ex() %>% check_code("#", fixed = TRUE)
CK Code: X1_Code_Started_04
# Use the sum() function to add the numbers 5, 10, 15
# Use the print() function to print the word "hello"
# Use the sum() function to add the numbers 5, 10, 15
sum(5, 10, 15)
# Use the print() function to print the word "hello"
print("hello")
ex() %>% {
check_function(., 'sum') %>% {
check_arg(., "...") %>% check_equal()
check_result(.) %>% check_equal()
}
check_function(., 'print') %>%
check_result() %>% check_equal()
}
CK Code: X1_Code_IntroR_01
# Run the code below by pressing Run
# Now debug the code - fix the mistake and press Run
# (When you have it correct don't forget to Submit)
Sum(1, 2)
# Run the code below by pressing Run
# Now try debugging the code - fix the mistake press Run again
sum(1, 2)
ex() %>% check_function('sum') %>%
check_result() %>% check_equal()
CK Code: X1_Code_IntroR_02
# This code will assign the number 20 to the R object my_favorite_number
my_favorite_number <- 20
# This code prints out my_favorite_number. Notice that you don't need to use the print() function to print the contents of an R object; you can just type the name of the object
my_favorite_number
# now revise the code to use your actual favorite number
# This is an example --- use your favorite number!
my_favorite_number <- 17
my_favorite_number
ex() %>% {
check_object(., "my_favorite_number")
check_output_expr(., "my_favorite_number")
}
CK Code: X1_Code_Save_01
# Assign 5 to num and 10 to NUM
num <-
NUM <-
# Write the name of the object that contains 10
# This will print out the contents of that object
num <- 5
NUM <- 10
NUM
msg_undefined <- "Make sure to define both variables: num and NUM."
msg_incorrect <- "Make sure you assign the correct value to each variable."
msg_not_print <- "Don't forget to print out the object that contains 10."
ex() %>% {
check_object(., 'num', msg_undefined) %>% check_equal(msg_incorrect)
check_object(., 'NUM', msg_undefined) %>% check_equal(msg_incorrect)
check_output_expr(., "NUM", missing_msg = msg_not_print)
}
CK Code: X1_Code_Save_02
# Here is the code to create two vectors my_vector and my_vector_2. We just made up those names.
# Run the code and see what happens
my_vector <- c(1, 2, 3, 4, 5)
my_vector_2 <- c(10, 10, 10, 10, 10)
# Now write some code to print out these two vectors.
# Here is the code to create two vectors my_vector and my_vector_2. We just made up those names.
# Run the code and see what happens
my_vector <- c(1, 2, 3, 4, 5)
my_vector_2 <- c(10, 10, 10, 10, 10)
# Now write some code to print out these two vectors.
my_vector # or print(my_vector)
my_vector_2 # or print(my_vector_2)
ex() %>% {
check_object(., 'my_vector')
check_object(., 'my_vector_2')
check_output_expr(., "my_vector")
check_output_expr(., "my_vector_2")
}
CK Code: X1_Code_Save_03
my_vector <- c(1, 2, 3, 4, 5)
# write code to multiply each number in my_vector by 100
my_vector <- c(1, 2, 3, 4, 5)
# write code to multiply each number in my_vector by 100
my_vector * 100
ex() %>% {
check_object(., "my_vector") %>%
check_equal()
check_operator(., "*") %>%
check_result() %>% check_equal()
}
CK Code: X1_Code_Save_04
require(coursekata)
# This creates my_vector and stores 1, 2, 3, 4, 5 in it
my_vector <- c(1, 2, 3, 4, 5)
# Now write code to save my_vector * 100 back into my_vector
my_vector_100 <-
# This creates my_vector and stores 1, 2, 3, 4, 5 in it
my_vector <- c(1, 2, 3, 4, 5)
# Now write code to save my_vector * 100 back into my_vector
my_vector_100 <- my_vector * 100
ex() %>% {
check_operator(., "*") %>%
check_result() %>% check_equal()
check_object(., "my_vector") %>%
check_equal()
}
CK Code: X1_Code_Save_05
require(coursekata)
my_vector <- c(1,2,3,4,5)
my_vector_100 <- my_vector * 100
# Write code to get the 4th value in my_vector
my_vector[4]
ex() %>% check_output_expr(
"my_vector[4]",
missing_msg = "Have you used `[4]` to print out the 4th number in `my_vector`?"
)
CK Code: X1_Code_Save_06
require(coursekata)
my_vector <- c(2, 4, 6, 8)
# Use sum() to total up the values in my_vector
sum(my_vector)
ex() %>% {
check_object(., "my_vector")
check_function(., "sum", not_called_msg = "don't forget to use the sum() function") %>%
check_result() %>%
check_equal(incorrect_msg = "did you call sum() on my_vector?")
}
CK Code: X1_Code_Save_07
require(coursekata)
# Try typing TipExperiment to see what is in the data frame.
# Try typing TipExperiment to see what is in the data frame.
TipExperiment
ex() %>% check_output_expr("TipExperiment")
CK Code: X1_Code_DataFrames_01
require(coursekata)
# Run this code to get the first 6 rows of TipExperiment
head(TipExperiment)
# Run this code to get the first 6 rows of TipExperiment
head(TipExperiment)
ex() %>% check_function("head") %>%
check_result() %>% check_equal()
CK Code: X1_Code_DataFrames_02
require(coursekata)
# Use glimpse() to see what’s in TipExperiment
# Use glimpse() to see what’s in TipExperiment
glimpse(TipExperiment)
ex() %>% check_function("glimpse") %>%
check_result() %>% check_equal()
CK Code: X1_Code_DataFrames_03
require(coursekata)
# Use the $ sign to print out the contents of the Condition variable in the TipExperiment data frame
# Use the $ sign to print out the contents of the Condition variable in the TipExperiment data frame
TipExperiment$Condition
ex() %>% check_output_expr(
"TipExperiment$Condition",
missing_msg = "Have you used $ to select the Condition variable in TipExperiment?"
)
CK Code: X1_Code_DataFrames_04
require(coursekata)
# Print out all the rows in which the variable Condition is equal to "Control"
# Print out all the rows in which the variable Condition is equal to "Control"
TipExperiment[TipExperiment$Condition == "Control", ]
ex() %>% check_output_expr(
'TipExperiment[TipExperiment$Condition == "Control", ]',
missing_msg = "Check your code -- something didn't match with the solution. A common mistake here is to forget the comma at the end"
)
CK Code: X1_Code_DataFrames_05
require(coursekata)
# Print out all the rows in which the variable Condition was "Smiley Face" tables and that also tipped less than 20 percent
# Print out all the rows in which the variable Condition was "Smiley Face" tables and that also tipped less than 20 percent
TipExperiment[TipExperiment$Condition == "Smiley Face" & TipExperiment$Tip < 20, ]
ex() %>% check_output_expr(
'TipExperiment[TipExperiment$Condition == "Smiley Face" & TipExperiment$Tip < 20, ]',
missing_msg = "Check your code -- something didn't match with the solution. A common mistake here is to forget the comma at the end"
)
CK Code: X1_Code_DataFrames_06
require(coursekata)
# save TipExperiment, arranged by Tip, back to TipExperiment
arrange(TipExperiment, Tip)
# write code to print out the first 6 rows of TipExperiment
# save TipExperiment, arranged by Tip, back to TipExperiment
TipExperiment <- arrange(TipExperiment, Tip)
# write code to print out the first 6 rows of TipExperiment
head(TipExperiment)
no_save <- "Make sure to both `arrange()` `TipExperiment` by `Tip` *and* save the arranged data frame back to `TipExperiment`."
ex() %>% {
check_object(., "TipExperiment") %>% check_equal(incorrect_msg = no_save)
check_function(., "head") %>% check_result() %>% check_equal()
}
CK Code: X1_Code_DataFrames_07
require(coursekata)
# Use the head() function to look at the first six rows of Ames
# Use the head() function to look at the first six rows of
head(Ames)
ex() %>% check_output_expr(
"head(Ames)",
missing_msg = "Did you call `head()` with `Ames`?"
)
CK Code: X1_Code_VarTypes_01
require(coursekata)
# this turns HasCentralAir into a factor
Ames$HasCentralAir <- factor(Ames$HasCentralAir)
# this code sums up Ames$HasCentralAir
sum(Ames$HasCentralAir)
# how would we fix this error? (read text below!)
# this turns HasCentralAir into a factor
Ames$HasCentralAir <- factor(Ames$HasCentralAir)
# this turns it back into a numbers
Ames$HasCentralAir <- as.numeric(Ames$HasCentralAir)
sum(Ames$HasCentralAir)
ex() %>% check_function("sum") %>%
check_result() %>% check_equal()
CK Code: X1_Code_VarTypes_02
require(coursekata)
# Modify this code
select(Ames, ...)
select(Ames, PriceK, PriceR, Neighborhood)
ex() %>% check_output_expr("select(Ames, PriceK, PriceR, Neighborhood)")
CK Code: X1_Code_Selecting_01
require(coursekata)
# Modify this code
filter()
# Modify this code
filter(Ames, PriceK > 300)
ex() %>% check_output_expr("filter(Ames, PriceK > 300)")
CK Code: X1_Code_Selecting_02
require(coursekata)
# Arrange Ames by GarageCars in descending order
Ames <-
# Use $ to print out the values of GarageCars from Ames
# Arrange Ames by GarageCars in descending order
Ames <- arrange(Ames, desc(GarageCars))
# Use $ to print out the values of GarageCars from Ames
Ames$GarageCars
ex() %>% {
check_function(.,"arrange")
check_function(., "desc")
check_object(., "Ames") %>% check_equal()
check_output_expr(., "Ames$GarageCars")
}
CK Code: X1_Code_Manipulating_01
require(coursekata)
Ames <- Ames %>%
arrange(desc(GarageCars))
# Modify this to filter for homes where GarageCars does not equal "NA"
Ames_subset <- filter(Ames, PriceK > 300)
# To check your work, this prints out the variable GarageCars from Ames_subset
# Do you see any NAs?
Ames_subset$GarageCars
# Modify this to filter for homes where GarageCars does not equal "NA"
Ames_subset <- filter(Ames, GarageCars != "NA")
# To check your work, this prints out the variable GarageCars from Ames_subset
# Do you see any NAs?
Ames_subset$GarageCars
ex() %>% {
check_function(., "filter") %>% {
check_arg(., ".data") %>% check_equal()
check_arg(., "...") %>% check_equal()
}
check_object(., "Ames_subset") %>% check_equal()
check_output_expr(., "Ames_subset$GarageCars", missing_msg = "Make sure to print out GarageCars from the Ames_subset data frame.")
}
CK Code: X1_Code_Manipulating_02
require(coursekata)
Ames <- Ames %>%
arrange(desc(GarageCars))
# try running this code
filter(Ames, is.na(GarageCars))
filter(Ames, is.na(GarageCars))
ex() %>% {
check_function(., "filter") %>%
check_arg("...") %>%
check_equal()
check_output_expr(., "filter(Ames, is.na(GarageCars))")
}
CK Code: X1_Code_Manipulating_03
require(coursekata)
Ames <- coursekata::Ames
# try running this
BuiltPre1900 <- Ames$YearBuilt < 1900
# take a glimpse at Ames to check if BuildPre1900 is in there
# fix the first line of code to put BuildPre1900 in Ames
# try running this
Ames$BuiltPre1900 <- Ames$YearBuilt < 1900
# take a glimpse at Ames to check if BuildPre1900 is in there
glimpse(Ames)
# fix the first line of code to put BuildPre1900 in Ames
ex() %>% {
check_object(., "Ames") %>%
check_column("BuiltPre1900") %>%
check_equal()
check_function(., "glimpse") %>%
check_result() %>%
check_equal()
}
CK Code: X1_Code_Manipulating_04
require(coursekata)
# This code creates a variable called BuiltPre1900
Ames$BuiltPre1900 <- Ames$YearBuilt < 1900
# Write code to tally up BuiltPre1900 in Ames in two different ways
# This code creates a variable called BuiltPre1900
Ames$BuiltPre1900 <- Ames$YearBuilt < 1900
# Write code to tally up BuiltPre1900 in Ames in two
tally(Ames$BuiltPre1900)
tally(~BuiltPre1900, data = Ames)
ex() %>% check_correct(
check_function(., "tally") %>%
check_result() %>% check_equal(),
{
check_error(.)
check_function(., "tally") %>%
check_arg("x") %>%
check_equal(incorrect_msg = "Make sure you are getting `BuiltPre1900` from `Ames` using the `$`.")
}
)
CK Code: X1_Code_Manipulating_05
require(coursekata)
# Save the current year by editing the code below
CurrentYear <- 1900
# Write code to create a variable that finds how old the house is
# Hint: CurrentYear is not in the Ames data frame so it won’t need Ames$ in front of it
Ames$HowOld <-
# This will print HowOld from the Ames data frame
Ames$HowOld
CurrentYear <- as.numeric(format(Sys.Date()[1],'%Y'))
Ames$HowOld <- CurrentYear - Ames$YearBuilt
ex() %>%
check_object("Ames") %>%
check_column("HowOld") %>%
check_equal()
CK Code: X1_Code_Manipulating_06
require(coursekata)
# try running this code
gf_histogram(~ PriceK, fill = "darkgray", data = Ames)
# try running this code
gf_histogram(~ PriceK, fill = "darkgray", data = Ames)
ex() %>% {
check_function(., "gf_histogram") %>% check_arg("object", arg_not_specified_msg = "Make sure to keep ~PriceK") %>% check_equal()
check_function(., "gf_histogram") %>% check_arg("data", arg_not_specified_msg = "Make sure to specify data") %>% check_equal()
check_function(., "gf_histogram") %>% check_result() %>% check_equal(incorrect_msg = "For this exercise, make sure not to change the code")
}
CK Code: X1_Code_Visualizing_01
require(coursekata)
# modify this code for 100 bins
gf_histogram(~ PriceK, bins = 30, data = Ames)
# modify this code for 100 bins
gf_histogram(~ PriceK, bins = 100, data = Ames)
ex() %>% check_function("gf_histogram") %>%
check_arg("bins") %>%
check_equal()
CK Code: X1_Code_Visualizing_02
require(coursekata)
# Create a bar graph of GarageType in the Ames data frame. Use the gf_bar() function
gf_bar(~ GarageType, data = Ames)
ex() %>% check_function("gf_bar") %>% {
check_arg(., "data") %>% check_equal(incorrect_msg="Don't forget to set `data = Ames`")
check_result(.) %>% check_equal(incorrect_msg = "Did you use `~GarageType`?")
}
CK Code: X1_Code_VisualizingCat_01
require(coursekata)
# Add arguments color and fill and width to this bar graph
gf_bar(~ GarageType, data = Ames)
# any values of arguments are acceptable
gf_bar(~ GarageType, color = "yellow", fill = "navyblue", width = .4, data = Ames)
ex() %>% {
check_function(., "gf_bar", index = 1) %>% {
check_arg(., "color")
check_arg(., "fill")
check_arg(., "width")
}
}
CK Code: X1_Code_VisualizingCat_02
require(coursekata)
# change this to a bar chart with proportions
# don’t change the fill color
gf_bar(~ GarageType, data = Ames, fill = "royalblue")
gf_props(~ GarageType, data = Ames, fill = "royalblue")
ex() %>% check_function("gf_props") %>%
check_result() %>% check_equal()
CK Code: X1_Code_VisualizingCat_03