## Course Outline

• segmentLearnosity
• segmentCKCode

### list Items Test Book

Book
• Items Test Book

## ckcode ⌲ chapter-a2-understanding-data

require(coursekata) # Here's how to combine nine 2s into a vector # You could also use rep(2, times = 9) bunch_of_2s <- c(2, 2, 2, 2, 2, 2, 2, 2, 2) # Create a vector called bunch_of_123s with the numbers # 2, 1, 3, 3, 2, 3, 1, 2, 1 bunch_of_123s <- c() # Here's how to combine nine 2s into a vector # You could also use rep(2, times = 9) bunch_of_2s <- c(2, 2, 2, 2, 2, 2, 2, 2, 2) # Create a vector called bunch_of_123s with the numbers # 2, 1, 3, 3, 2, 3, 1, 2, 1 bunch_of_123s <- c(2, 1, 3, 3, 2, 3, 1, 2, 1) ex() %>% check_object("bunch_of_123s") %>% check_equal()
CK Code: ch2-1
require(coursekata) # Here is code to create the vector that we named bunch_of_2s bunch_of_2s <- c(2,2,2,2,2,2,2,2,2) # Now, let's run the tally() function on bunch_of_2s # Here is code to create the vector that we named bunch_of_2s bunch_of_2s <- c(2,2,2,2,2,2,2,2,2) # Now, let's run the tally() function on bunch_of_2s tally(bunch_of_2s) ex() %>% check_function("tally") %>% check_result() %>% check_equal()
CK Code: ch2-2
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # Try typing MindsetMatters to see what is in the data frame. MindsetMatters ex() %>% check_output_expr("MindsetMatters")
CK Code: ch2-3
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # Run this code to get the first 6 rows of MindsetMatters head(MindsetMatters) # Run this code to get the first 6 rows of MindsetMatters head(MindsetMatters) ex() %>% check_function("head") %>% check_result() %>% check_equal()
CK Code: ch2-4
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # Run this code to see the structure of MindsetMatters str(MindsetMatters) str(MindsetMatters) ex() %>% check_function("str") %>% check_result() %>% check_equal()
CK Code: ch2-5
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # Use the $sign to print out the contents of the Age variable in the MindsetMatters data frame # Use the$ sign to print out the contents of the Age variable in the MindsetMatters data frame MindsetMatters$Age ex() %>% check_output_expr("MindsetMatters$Age", missing_msg = "Have you used $to select the Age variable in MindsetMatters?") CK Code: ch2-27 require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # Use tally() with the MindsetMatters data frame to create a frequency table of housekeepers by Condition # Use tally() with the MindsetMatters data frame to create a frequency table of housekeepers by Condition tally(~Condition, data = MindsetMatters) # Another solution # tally(MindsetMatters$Condition) ex() %>% check_function("tally") %>% check_result() %>% check_equal()
CK Code: ch2-6
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # save MindsetMatters, arranged by Age, back to MindsetMatters arrange(MindsetMatters, Age) # write code to print out the first 6 rows of MindsetMatters # save MindsetMatters, arranged by Age, back to MindsetMatters MindsetMatters <- arrange(MindsetMatters, Age) # write code to print out the first 6 rows of MindsetMatters head(MindsetMatters) no_save <- "Make sure to both arrange() MindsetMatters by Age *and* save the arranged data frame back to MindsetMatters." ex() %>% { check_object(., "MindsetMatters") %>% check_equal(incorrect_msg = no_save) check_function(., "arrange") %>% check_arg("...") %>% check_equal() check_function(., "head") %>% check_result() %>% check_equal() }
CK Code: ch2-7
require(coursekata) MindsetMatters <- Lock5withR::MindsetMatters %>% mutate(Condition = factor(Cond, levels = c(1, 0), labels = c("Informed", "Uninformed"))) # arrange MindsetMatters by Wt in descending order MindsetMatters <- # write code to print out a few rows of MindsetMatters # arrange MindsetMatters by Wt in descending order MindsetMatters <- arrange(MindsetMatters, desc(Wt)) # write code to print out a few rows of MindsetMatters head(MindsetMatters) no_save <- "Did you save the arranged data set back to MindsetMatters?" ex() %>% { check_function(., "desc") %>% check_arg("x") %>% check_equal(eval = FALSE) check_function(., "arrange") %>% check_arg(".data") %>% check_equal(eval = FALSE) check_object(., "MindsetMatters") %>% check_equal(incorrect_msg = no_save) check_function(., "head") %>% check_arg("x") %>% check_equal() }
CK Code: ch2-8
require(coursekata) Fingers <- Fingers %>% mutate_if(is.factor, as.numeric) %>% arrange(desc(Sex)) %>% {.[1, "FamilyMembers"] <- 2; . } %>% {.[1, "Height"] <- 62; . } # A way to look at a data frame is to type its name # Look at the data frame called Fingers Fingers ex() %>% check_output_expr("Fingers")
CK Code: ch2-9
require(coursekata) Fingers <- Fingers %>% mutate_if(is.factor, as.numeric) %>% arrange(desc(Sex)) %>% {.[1, "FamilyMembers"] <- 2; . } %>% {.[1, "Height"] <- 62; . } # Remember the head() command? # Use it to look at the first six rows of Fingers head(Fingers) ex() %>% check_output_expr("head(Fingers)", missing_msg = "Did you call head() with Fingers?")
CK Code: ch2-10
require(coursekata) Fingers <- Fingers %>% mutate_if(is.factor, as.numeric) %>% arrange(desc(Sex)) %>% {.[1, "FamilyMembers"] <- 2; . } %>% {.[1, "Height"] <- 62; . } # Try it and see what happens head(Fingers, 3) head(Fingers, 3) ex() %>% check_function("head") %>% check_arg("n") %>% check_equal()
CK Code: ch2-11
require(coursekata) Fingers <- Fingers %>% mutate_if(is.factor, as.numeric) %>% arrange(desc(Sex)) %>% {.[1, "FamilyMembers"] <- 2; . } %>% {.[1, "Height"] <- 62; . } # Try using tail() to look at the last 6 rows of the Fingers data frame. tail(Fingers) ex() %>% check_function("tail") %>% check_result() %>% check_equal()
CK Code: ch2-12
require(coursekata); allow_solution_error() Fingers <- Fingers %>% mutate_if(is.factor, as.numeric) %>% arrange(desc(Sex)) %>% {.[1, "FamilyMembers"] <- 2; . } %>% {.[1, "Height"] <- 62; . } # this turns Sex into a factor: Fingers$Sex <- factor(Fingers$Sex) # click <submit>: this code will produce an error because you can't sum a factor sum(Fingers$Sex) # this turns Sex into a factor: Fingers$Sex <- factor(Fingers$Sex) # click <submit>: this code will produce an error because you can't sum a factor sum(Fingers$Sex) ex() %>% check_function("sum") %>% check_output("not meaningful for factors")
CK Code: ch2-13
require(coursekata) Fingers <- Fingers %>% mutate_if(is.factor, as.numeric) %>% arrange(desc(Sex)) %>% {.[1, "FamilyMembers"] <- 2; . } %>% {.[1, "Height"] <- 62; . } # Interest has been coded numerically in the Fingers data.frame # Modify the following to convert it to factor and store it as InterestFactor in Fingers Fingers$InterestFactor <- Fingers$InterestFactor <- factor(Fingers$Interest) ex() %>% check_object("Fingers") %>% check_column("InterestFactor") %>% check_equal() CK Code: ch2-14 require(coursekata) fake_pop <- rep(0:9, each = 100) # This takes a random sample of 10 numbers # from fake_pop and saves them in random_sample random_sample <- sample(fake_pop, 10) # This makes a histogram of your sample gf_histogram(~ random_sample, binwidth=1) + scale_x_continuous(limits = c(-.5, 9.5), breaks = c(0:9))  random_sample <- sample(fake_pop, 10) # this will make a histogram of your sample gf_histogram(~ random_sample, binwidth=1) + scale_x_continuous(limits = c(-.5, 9.5), breaks = c(0:9)) ex() %>% { check_object(., "random_sample") check_function(., "gf_histogram") } CK Code: ch2-17 require(coursekata) # Run this code select(Fingers, Sex, Thumb) select(Fingers, Sex, Thumb) ex() %>% check_output_expr("select(Fingers, Sex, Thumb)") CK Code: ch2-28 require(coursekata) # Write the code select(Fingers, Sex, Thumb) inside of head() # as shown in the .gif above head() head(select(Fingers, Sex, Thumb)) ex() %>% check_or( check_correct( check_function(., "head") %>% check_result() %>% check_equal(), check_correct( check_function(., "select") %>% check_result() %>% check_equal(), check_function(., "select") %>% { check_arg(., ".data") %>% check_equal(incorrect_msg = "Did you specify the Fingers data frame?") check_arg(., "...", arg_not_specified_msg = "Did you include the column names?") %>% check_equal(incorrect_msg = "Did you select the Sex and Thumb columns?") } ) ), override_solution(., "head(select(Fingers, Thumb, Sex))") %>% check_correct( check_function(., "head") %>% check_result() %>% check_equal(), check_correct( check_function(., "select") %>% check_result() %>% check_equal(), check_function(., "select") %>% { check_arg(., ".data") %>% check_equal(incorrect_msg = "Did you specify the Fingers data frame?") check_arg(., "...", arg_not_specified_msg = "Did you include the column names?") %>% check_equal(incorrect_msg = "Did you select the Thumb and Sex columns?") } ) ) ) CK Code: ch2-29 require(coursekata) # Run this code filter(Fingers, Thumb == 66) filter(Fingers, Thumb == 66) ex() %>% check_output_expr("filter(Fingers, Thumb == 66)") CK Code: ch2-30 require(coursekata) # Arrange SSLast in descending order Fingers <- # Print out just the variable SSLast from the Fingers data frame Fingers <- arrange(Fingers, desc(SSLast)) Fingers$SSLast ex() %>% { check_function(.,"arrange") check_function(., "desc") check_object(., "Fingers") %>% check_equal() check_output_expr(., "Fingers$SSLast") } CK Code: ch2-18 require(coursekata) Fingers <- Fingers %>% arrange(desc(SSLast)) # Filter out the students who have missing data for SSLast Fingers_subset <- # Print out the variable SSLast from Fingers_subset Fingers_subset <- filter(Fingers, SSLast != "NA") Fingers_subset$SSLast ex() %>% { check_function(., "filter") %>% { check_arg(., ".data") %>% check_equal() check_arg(., "...") %>% check_equal() check_result(.) %>% check_equal() } check_or(., check_output_expr(., "Fingers_subset$SSLast"), override_solution(., 'Fingers_subset <- filter(Fingers, SSLast != "NA"); select(Fingers_subset, SSLast)') %>% check_function("select") %>% check_result() %>% check_equal() ) } CK Code: ch2-19 require(coursekata) Fingers$RingLonger <- Fingers$Ring > Fingers$Index # This code creates a variable called RingLonger Fingers$RingLonger <- Fingers$Ring > Fingers$Index # Write code to tally up RingLonger in Fingers. # Either of these: tally(Fingers$RingLonger) tally(~RingLonger, data = Fingers) 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 RingLonger from Fingers using the $.") } ) CK Code: ch2-20 require(coursekata) # Write code to create this summary variable Fingers$IndexRingRatio <- # Will this print anything? Fingers$IndexRingRatio <- Fingers$Index / Fingers$Ring ex() %>% check_object("Fingers") %>% check_column("IndexRingRatio") %>% check_equal() CK Code: ch2-21 require(coursekata) Fingers <- Fingers %>% mutate(IndexRingRatio = Index/Ring) # Use head() and select() together to look at the first six rows of Ring, Index, and IndexRingRatio head(select(Fingers, Ring, Index, IndexRingRatio)) # These also work: # select(Fingers, Ring, Index, IndexRingRatio) %>% head() # Fingers %>% select(Ring, Index, IndexRingRatio) %>% head() check_df <- function(state) { check_function(state, "head") %>% check_result() %>% check_equal() } ex() %>% check_or( check_df(.), override_solution(., "head(select(Fingers, Ring, IndexRingRatio, Index))") %>% check_df(.), override_solution(., "head(select(Fingers, Index, Ring, IndexRingRatio))") %>% check_df(.), override_solution(., "head(select(Fingers, Index, IndexRingRatio, Ring))") %>% check_df(.), override_solution(., "head(select(Fingers, IndexRingRatio, Ring, Index))") %>% check_df(.), override_solution(., "head(select(Fingers, IndexRingRatio, Index, Ring))") %>% check_df(.) ) CK Code: ch2-21a require(coursekata) # This code averages the lengths of the Thumb and Pinkie # Modify it to find the average length of all five fingers Fingers$AvgFinger <- (Fingers$Thumb + Fingers$Pinkie)/2 # Write code to look at a few lines of the Fingers data frame Fingers$AvgFinger <- (Fingers$Thumb + Fingers$Index + Fingers$Middle + Fingers$Ring + Fingers$Pinkie)/5 head(Fingers) ex() %>% { check_object(., "Fingers") %>% check_column("AvgFinger") %>% check_equal() check_function(., "head") %>% check_arg("x") %>% check_equal() }
CK Code: ch2-22
require(coursekata) Fingers <- Fingers %>% mutate(Job = as.numeric(Job)) # Save the recoded version of Job to JobRecode Fingers$JobRecode <- recode() # Write code to print the first 6 observations of Job and JobRecode Fingers$JobRecode <- recode(Fingers\$Job, "1" = 0, "2" = 50, "3" = 100) head(select(Fingers, Job, JobRecode)) ex() %>% { check_object(., "Fingers") %>% check_column("JobRecode") %>% check_equal() . %>% check_or( check_output_expr(., "head(select(Fingers, Job, JobRecode))"), check_output_expr(., "head(select(Fingers, JobRecode, Job))") ) }
CK Code: ch2-23
require(coursekata) # run your code here
CK Code: A2_Code_Review2_01
require(coursekata) # run your code here
CK Code: A2_Code_Review2_02
require(coursekata) # run your code here
CK Code: A2_Code_Review2_03
require(coursekata) # run your code here
CK Code: A2_Code_Review2_04
require(coursekata) # run your code here
CK Code: A2_Code_Review2_05
require(coursekata) # run your code here
CK Code: A2_Code_Review2_06
require(coursekata) # run your code here
CK Code: A2_Code_Review2_07
require(coursekata) # run your code here
CK Code: A2_Code_Review2_08