The purpose of this assignment is to help you get familiar with R studio, R markdown, and learn a little about the R language.
This is an R markdown file. It has a section at that top with the document information, it has these sections where you can write normal text, and it has code chunks.
A code chunk is a section that starts with 3 backticks and ends with 3 backticks. There is a code chunk directly below this paragraph. Anything inside a code chunk will be interpreted as code, not normal text. To run a code chunk, click the green arrow on the upper right hand corner of it. Run this code chunk below now.
2+2
## [1] 4
What is 4657 divided by 37? Use the code chunk below to solve by entering your math operation between the backticks then pressing the green arrow to run it.
4657/37
## [1] 125.8649
To do things more complex than a single line, we need to assign
values to objects. We can then use the objects similar to how you use
variables in algebra. To do this we use the assignment operator:
<-
x <- 45
my_number <- 100
x*my_number
## [1] 4500
There are many kinds of objects. Above we assigned numbers to our
objects and that made them numeric so we could multiply
them together. We can assign strings of characters to objects to.
word1 <- "Cat"
word2 <- "Dog"
word2
## [1] "Dog"
The quotes used around “Cat” and “Dog” are essential. Without them, R
would think we are assigning an object called Cat to
word1, not the word “Cat”.
Also note that we called word2 to get it to print “Dog”.
We can also get R to print the result of an assignment by putting ()
around the whole line.
word3 <- "Snake" # This one doesn't print anything
(word4 <- "Bird") # This one prints the value of word4
## [1] "Bird"
Notice the comments in that code chunk. Anything after a hashtag is ignored by the interpreter.
R also has functions. A function takes an argument. Here the
sqrt() function takes the square root of the argument:
sqrt(25)
## [1] 5
Functions can take objects as arguments. Use the sqrt
function but give it the my_number object. Remember we
assigned the value of 100 to my_object in a code chunk
above.
sqrt(my_number)
## [1] 10
We can also print using the print() function to print
things:
print(word3)
## [1] "Snake"
This can especially be useful when combined with the
paste() function that takes a series of arguments and
pastes them together. Here we are using it to paste some text together
with our word3 then passing that combined sentence to the
print() function:
print(paste("This is the third word:", word3))
## [1] "This is the third word: Snake"
You now know 3 different ways to print. Assign a word to a new object
called word5 and then print that word using your choice of
method.
word4 <- "Nova"
print(word4)
## [1] "Nova"
Vectors are the most common and most basic data structure in R.
Vectors are 1 dimensional collections of data in R, like a single row or
column of a spreadsheet. We can use the c() function to
list out the items we want to go into our vector, like this:
(acids <- c("HCl", "HF", "H2SO4", "HNO3"))
## [1] "HCl" "HF" "H2SO4" "HNO3"
acids is now a vector holding 4 sets of characters.
Vectors can take any kind of object. Notice we used parenthesis around
the whole line so it printed the output. Here is a numeric vector:
(concentrations <- c(1, 1.4, 0.4, 0.8))
## [1] 1.0 1.4 0.4 0.8
Try multiplying the concentrations vector by 2:
2*concentrations
## [1] 2.0 2.8 0.8 1.6
Because the vector was numeric we could do number things
with it.
Now try multiplying the acids vector by 2:
We got an error because we tried to do numeric things to words. Errors happen all the time. The error message will tell you what went wrong, although they can often be a bit cryptic.
Any errors left in this document will be problematic at the end, so
delete the line you wrote above to multiply acids by 2.
Create a vector list of 4 bases:
(bases <- c("NaOH", "KOH", "H2O", "MgOH"))
## [1] "NaOH" "KOH" "H2O" "MgOH"
A data frame is a 2 dimensional data structure composed of rows and
columns, like a spreadsheet. Here we are creating one out of our
acids and concentrations vectors:
df <- data.frame(acids, concentrations)
print(df)
## acids concentrations
## 1 HCl 1.0
## 2 HF 1.4
## 3 H2SO4 0.4
## 4 HNO3 0.8
We can use the $ symbol to call a single column of our data frame as
a vector. Here we are calling the acids vector from our
df data frame:
df$acids
## [1] "HCl" "HF" "H2SO4" "HNO3"
We can add a column to a data frame by assigning some values to it:
df$price <- c(5, 30, 12, 18 )
print(df)
## acids concentrations price
## 1 HCl 1.0 5
## 2 HF 1.4 30
## 3 H2SO4 0.4 12
## 4 HNO3 0.8 18
Multiply the price vector of our df data
frame by 3:
3*df$price
## [1] 15 90 36 54
Extra challenge: Add your bases vector as a new column
to the df data frame, then print the data frame.
df$bases <- c(bases)
print(df)
## acids concentrations price bases
## 1 HCl 1.0 5 NaOH
## 2 HF 1.4 30 KOH
## 3 H2SO4 0.4 12 H2O
## 4 HNO3 0.8 18 MgOH
Finally, let’s knit this document. Knitting an R markdown document generates a polished report document. Knit this one now by clicking the Knit button at the top of this window, or by going to File -> Knit Document.