Lab 3 is called R Basics. In the first two labs you have been introduced to R and R-studio with working example code for graphing and descriptive statistics. This lab is intended to familiarize you with some basic R operations.
Instructions: Below you will find two things. Screencasts and some lists of problems to learn about and solve. The screencasts show how to solve all the problems.
Your task will be to create a blank .Rmd file, and then solve as many of these problems as you can. Submit your .rmd file on blackboard. This lab is intended to help resolve some basic issues and errors that might crop up when you use R and R-studio. Your .rmd file simply has to show that you worked through the lab.
In our class we are primarily working with .Rmd files, otherwise called R markdown files. R markdown files provide a way to write plain language as well as code, and have your notes, your code, and the output of your code be compiled to a document so that everything is one place. There are other ways to write code in R, including using regular R scripts, these have a .R ending. Although we don’t use .R scripts in the lab, it is worth knowing what they are. You should be able to:
Add comments to a code block
run code from an r script
library()
library(data.table)
and the fread()
functionR code provides a recipe for completing operations. These operations usually need to be completed in order from first to last. In the lab assignments, there are lots of examples of code from the beginning of the lab to the end of the lab. All of the code will work when the code is run in order from first to last. If you are trying to run some code near the end, but have not first run the code blocks at the beginning, this could be one reason why your code isn’t running. Watch the screencast to see some examples
If you run a line a code in the console to make a variable contain some numbers, where does the variable get stored?
If you run a line of code from an .rmd file to make a variable contain some numbers, where does the variable get stored?
If you run a line of code from an .rmd file by knitting the .rmd file, where does the variable get stored?
rep()
to replicate something many timesrep(x=1, times=5)
rep(1,5)
rep(300,4)
rep(c(1,2,3),2)
rep(c(1,2,3),each=2)
rep("words",4)
seq()
to make a sequence of numbersseq(1,5,1)
seq(from=100, to=10, by= -10)
rep(seq(1,3,1),times=2)
Some discussion of how we have used dplyr so far up to lab 2