CS计算机代考程序代写 —


title: “Introduction to R (DataCamp) Exercises”
author: “
date: “
output: html_document

##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##
# Prerequisite: DataCamp course Introduction to R
# Load this Rmd file in RStudio for your self-assessment after the above course.
# This will also get you accustomed to RStudio which you will use for self-assessment (and several courses).
# The answers are available in “Introduction to R (DataCamp) Exercises Answers.Rmd”
# Before you start (and to get to get accustomed to it for the Coding Prerequisite Exam):
# Enter your name and today’s date in lines 3 and 4
##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##

##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##
# This line of code (knitr…) is generated by RStudio.
# It allows you to submit (knit) your output.
# We will use an html document for output as specified in line 5 (more later on this). Just remember that when you encounter the “knit” instrution.
##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##
“`{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
“`

“`{r}
# Do not modify anything in this code block
# Run these lines of code by using the arrow button at the top right of this code chunk
grade1 <- 111 grade2 <- 11 average1 <- 111.1 average2 <- 1.1 ``` # Intro to Basics ## How it works ## Arithmetic with R ```{r} # Do not modify anything in this code block # Run these lines of code to see the values you set above grade1 grade2 average1 average2 ``` ```{r} # Print grade1 plus grade2 # Tip, use the the arrow button at the top right of this code chunk to run your code after you type in your answer ``` ```{r} # Print average1 minus grade1 ``` ```{r} # Print average2 times grade2 average2 * grade2 ``` ```{r} # Print grade1 divided by grade2 (using integer division) ``` ```{r} # Print grade1 divided by grade2 (using regular division) ``` ```{r} # Print average1 divided by grade1 (using regular division) ``` ```{r} # Print remainder of grade1 by grade2 ``` ```{r} # Print grade2 raised to the power of average2 ``` ## Variable assignment ```{r} # Assign the product of grade1, grade2, average1, average2 to variable called myAnswer ``` ```{r} # Print out the value of the variable myAnswer ``` ## Basic data types in R ## What's that data type? ```{r} # Do not modify anything in this code block # Run these lines of code by using the arrow button at the top right of this code chunk yes <- FALSE one <- 1.1 two <- "two" ``` ```{r} # Print out the types of each one the above variables # Tip: These are three of the primitive types of R ``` # Vectors ## Create a vector ```{r} # Do not modify anything in this code block # This code creates four vectors, exam1, exam2, exam3, and exam4. # Side note: This data was generated using pseudorandom generator runif # Google runif to learn more function (but you do not need to understand it works at this time) exam1 <- c(as.integer(runif(8,80,100))) exam2 <- c(as.integer(runif(8,80,100))) exam3 <- c(as.integer(runif(8,80,100))) exam4 <- c(as.integer(runif(8,80,100))) ``` # Naming a vector ## Calculating totals, etc. ```{r} # Create and print a vector aveExamScore containing the average for each exam. # Hint: This vector will contain an element for each exam. ``` ```{r} # Create and print a vector aveStudentScore with values containing the average for each student. ``` ```{r} # Create a vector students with the name of the students (Athy, Azhar, Bolon, Boulding, Erickson-Ludwig, McClarnon, Morgan, Petranka) # Protip: Cut-n-pasted as much as possible (such as the names list) to save time and improve accuracy # Hint: If you don't get it right the first time, think carefully about the difference between a variable name and the value stored in it. # Hint 2: Here's a quote from William Edward Hickson: "If at first you don't succeed, try, try again." ``` ```{r} # Assign the student names from students vector to aveStudentScore # Print aveStudentScore to validate ``` ```{r} # Compute and print meanAveScore that contains the average of the average scores # Hint: Use aveStudentScore ``` ## Selection by comparison ```{r} # Create and print a logical vector called aboveAve # The aboveAve is TRUE for students who are above average # The aboveAve is FALSE for students who are not above average ``` ```{r} # Print the scores of all students who have above average scores using aveStudentScore and aboveAve ``` ```{r} # Print the scores of all students who have below average scores using aveStudentScore, aveStudentScore, and # Try to do this in one statement using square brackets if you can ``` ```{r} # Now, let's re run the code so you can: # 1. See how to run all previous code chunks # 2. Learn how randomization works # Rerun all previous code chunks using the downarrow on the top right corner of this code chunk. # No code new code is required to answer this question. ``` # Matrices ## What's a matrix? ## Analyze matrices, you shall ## Naming a matrix ## Calculating ```{r} # Construct and print a matrix *scoresMatrix* that contains all exam scores # examN scores in column N (for N = 1, 2, 3, and 4) ``` ```{r} # Rename the columns of scoresMatrix from exam1, exam2, exam3, and exam4 # to Exam 1, Exam 2, Exam 3, Exam 4 # Print scoresMatrix to validate your change # Hint: Recall when you set rownames if your first attempt gives you an error. ``` ```{r} # Name the rows with the names of the students # Print scoresMatrix to validate your change # Hint: students vector was defined earlier ``` ```{r} # Calculate the total score of each student and store the result in totalStudentScore # Print totalStudentScore to validate your change ``` ```{r} # Append the totalStudentScore to scoresMatrix as its fifth column # Reprint scoresMatrix to validate your change ``` ```{r} # Append to scoresMatrix, the average score for each exam as the ninth row of scoresMatrix # Reprint scoresMatrix to validate your change ``` ```{r} # Select scoresMatrix's fourth column and store the result in a vector called finalExam (for all rows) # Print finalExam to validate your change ``` ```{r} # Add finalExam to scoresMatrix's 5th column (totalStudentScore) and store the result back into scoresMatrix's 5th column (totalStudentScore) # Hint: You are double weighting the Exam 4 score ``` ```{r} # Add another column averageStudentScore to scoresMatrix that has the adjusted average score # Tip: Be careful choosing the divisor (your adjusted average score should not be over 100) # Reprint scoresMatrix to validate your change ``` # Factors ## What's a factor and why would you use it? ## Summarizing a factor ## Factor levels ## Summarizing a factor ## Ordered factors ## Comparing ordered factors ```{r} # Create a vector teamName that records the team number for each student. # The first 4 elements of teamName are "Team 1" and the next four were in "Team 2". # For the last element is "none" because it corresponds to the average for each column. ``` ```{r} # Create and print a vector teamNumber of type unordered factor # That records the team with the three possible values of teamName as levels ``` ```{r} # Factors can be ordered as well. # Create and print teamRank that orders the teams based on the averageStudentScore in scoresMatrix. # You can do this in 3 steps: # 1. Find the average of averageStudentScore of each team based on scoresMatrix. # 1a. The first 4 rows correspond to Team 1 and the next 4 correspond to Team 2. # 2. Define an ordered factor so the team with the highest order is "greater" than the other. # 2a. You can use none for the lowest team rank. # 3. Print teamRank # Hint for Step 2: You will set the levels = c("none", "Team 2", "Team 1")) or levels = c("none", "Team 1", "Team 2")) ``` ```{r} # Uncomment & run the following two lines of code to understand the difference between ordered and unordered factors. # After running them, comment out the one that gives an error using # sign. # (Otherwise you will get an error when you knit/submit your work). # The problem is that teamNumber is unordered so it can't be compared based on an order. #teamRank[1] < teamRank[2] #teamNumber[1] < teamNumber[2] ``` # Data frames ## What's a data frame? ## Quick, have a look at your data set ## Have a look at the structure ## Creating a data frame ## Selection of data frame elements ## Filtering data frame ## Sorting your data frame ```{r} # Wouldn't it be nice to have scoresMatrix, teamName, and teamRank all in one matrix? # Try to combine scoresMatrix, teamName, and teamRank all in one matrix and store the result in scoresAndMoreMatrix. ``` ```{r} # Oops! That didn't work a single matrix can't accommodate both numeric and character # Note the quotes around all items (now everything in scoresMatrix is a character) # Dataframes overcome this limitation. # Now, create and print a dataframe scoresDF that combines scoresMatrix, teamName, and teamRank all in one. ``` ```{r} # Print out the first few rows of scoresDF ``` ```{r} # Print out the structure of scoresDF ``` ```{r} # Print out the summary statistics of scoresDF (using the summary command) ``` ```{r} # Print the fourth row and fifth column of scoresDF ``` ```{r} # Print all columns of scoresDF's fourth row ``` ```{r} # Print all rows of scoresDF's fifth, sixth, and seventh column. ``` ```{r} # Print the scoresDF's first four rows of the column named averageStudentScore ``` ```{r} # Create a vector inTeam1 that is true for all members of Team 1 ``` ```{r} # Print out only rows where team1Names is TRUE ``` ```{r} # Determine the sorted order of scoresMatrix with the highest score first. # Hint: Sorted order is determined by a variable decreasing (which is FALSE by default). # Tip: Google for an example as this is not covered in your DataCamp tutorial. ``` # Lists ## Lists, why would you need them? ## Creating a list ## Creating a named list ```{r} # Construct and print a list called list1Student with these three elements: # 1. The name of the first student in scoresMatrix (recall rownames is the names vector) # 2. All scores of the first student from the scoresMatrix # 3. The teamName of the first student from scoresDF # Do not name any elements in the list ``` ```{r} # The above list is confusing because of [[1]] etc. # Reconstruct list1Student with names of each element as specified below: # 1. student # 2. studentScores # 3. team ``` # Submitting your work ```{r} # Finally, look for the "Knit" in the top bar of the tab. # Click on that knit button to save an html of your code and output to an html file in the same directory as your Rmd file. # You don't need to submit the self-assessment but you will need to submit the Exam. # Note: Don't worry about inaccuracy in your teamRank order in your knitted file. # (This inaccuracy is due to a workaround we used since if-else statements are not covered in the Intro module.) ```