R Practice
18 Apr 2023TAKE a BREAK
If you have just finished swirl()
and how to clone your github repo in Rstudio, take a break before continuing.
Practice what you learned from swirl()
The exercises below will give you additional practice with the building blocks that you learned in swirl()
Open up the Assignment_3_template_1.Rmd
file in your repository and work from there. Save, commit, and push your file changes to the github repository after you complete each exercise.
One function in R is sum()
, which sums it arguments.
Exercise 1: Use sum() to determine the sum of numbers from 2000 to 20000. Provide your code and the actual sum in your answer.
Objects
In swirl
you learned about variables. Another (and more inclusive) name for variables is objects. Objects in R are used to store data, results, and even functions. The following should be familiar to you:
a <- 5
b <- 2:20
a
b
Exercise 2: In one or two sentences, describe what the above code snippet did.
You can list the objects in your workspace by using the ls()
function. Objects can be removed using the rm()
function. Note the important difference between R and Linux: in Linux ls
and rm
operate on files saved on disk. in R ls()
and rm()
operate on objects (variables) saved in R’s memory.
Objects are also shown in the upper right-hand pane if you click on the “Environment tab”.
To get started with ls()
and rm()
, try:
d <- "I just want to be deleted"
d
ls() #note that you have to include the parentheses for a function even if no arguments are needed.
rm(d)
ls()
d #generates an error because we removed it
Note that in the above snippet I again used the comment character “#”. Any text that follows a “#” is considered a comment and is not interpreted. I highly recommend extensively commenting your code to explain what you are doing. What makes sense today will seem like gibberish tomorrow. Comments can help.
Exercise 3: Add the contents of a and b together and place the results in a new object. Examine the result. Include your code. Try using both sum()
and +
; do you get different results? If so, why?
In the swirl()
tutorial you learned how to use the brackets [] to extract elements from objects. Lets practice here.
Exercise 4: What is the sum of the 5th through 10th element of object b? Provide your code and the sum.
Exercise 5: What is the sum of the 3rd, 8th, and 10th element of b?
For both of these exercises you should only need to have “b” in your code once.
2 Dimensional Objects
You were also introduced to 2-dimensional objects in swirl()
. Lets get a bit more practice with them here. The matrix command makes 2-dimensional objects. When extracting from a multi-dimensional object you must specify both dimensions within the square brackets.
m <- matrix(data=1:25,ncol=5,byrow=T)
m
m[5,5]
Exercise 6: When extracting from a 2-dimensional object, which number specifies rows and which specifies columns? What does m[3,]
do? How can you extract the 3rd, 4th and 5th columns of m together as one object?
Type cbind(m,101:105)
.
Exercise 7: What does the cbind command do? How about rbind? Create a new object “n” where the first row is a new row of numbers (your choice) and the following rows are the rows from matrix m. Optional: do the same but reverse the order of the rows from matrix m.
Exercise 8:
Copy or move your Swirl Notes into the repo, add and commit them.
Enter the file name(s) where prompted
Turning in the assignment
- Click the Knit button to generate an up-to-date html file of your markdown document. Check it to make sure you are happy with its content.
- Add your .Rmd and .html files to the repository and commit the changes.
- Push the changes
Preview the html on github
If you want to confirm that the correct html file was uploaded:
- Click on the html file on github.
- Click on the “raw” button (right hand side of the screen)
- Copy the URL at the top of the next page (URL should start with “raw.”)
- Go to https://htmlpreview.github.io and paste in the link (use the same browser you are using in step 3)