1 + 1
[1] 2
2 / 4
[1] 0.5
3^2
[1] 9
The first thing we will learn is how to use R as a calculator. You can use any of the math operators you want:
+
Addition-
Subtraction*
Multiplication/
Division^
ExponentiationLet’s experiment with some arithmetic expressions:
1 + 1
[1] 2
2 / 4
[1] 0.5
3^2
[1] 9
Order of operations (via PEMDAS) apply here too:
5 + 2 * 3
[1] 11
This does 2 * 3 first and then adds 5 to get 11. If we want to do 5 + 2 first, then we can wrap it in parenthesis (the P in PEMDAS):
5 + 2) * 3 (
[1] 21
There are even some operators that you might not know that have to do with remainders:
# Get the remainder
13 %% 2
[1] 1
# Divide and round down to the nearest decimal
13 %/% 2
[1] 6
Compute the sample average of the following sample of baby weights (in lbs.):
(7.7, 8.2, 8.3, 7.6, 9.2, 7.4, 11.1)
R is based around functions. A function takes an input (or multiple inputs) and produces an output. There are many many functions in R, but first lets learn some calculator type functions. For example, if I want to take the square root, I can use the function sqrt
. Here are some example of math functions:
The form of a function call is function_name(arguments)
sqrt
, abs
, factorial
(
)
For example sqrt(16)
says to take the argument 16
and apply the function sqrt
of it.
log()
Variables are immensely helpful in R. It lets you store values by giving them a name
and then lets you access the variables later by name. I can assign variables using either <-
or =
.
Create variable x
with value 5 and a variable y
with value 20.
<- 5
x <- 20 y
What is the sum of x
and y
?
+ y x
[1] 25
Note the form of creating the variable:
x
and y
<-
or =
The reason behind the left arrow is that the arrow points to variable name where we want to put the value into.
You can create a variable containing text by using ""
<- "Kyle Butts"
instructor_name print(instructor_name)
[1] "Kyle Butts"
We can use the cat
command to print out the text to the console:
cat(instructor_name)
Kyle Butts
Use quotation marks to create a string and call it my_name
.
<- "Kyle Butts"
my_name my_name
[1] "Kyle Butts"
Often time we might want to combine text from multiple sources and/or add data to our strings. We can use the paste
/paste0
functions to combine together strings. paste0
will append the strings as written, while paste
will automatically add a space between each thing it is concatenating. Dealer’s choice for which you prefer
For example,
paste0("Kyle", "Butts")
[1] "KyleButts"
paste("Kyle", "Butts")
[1] "Kyle Butts"
What is cool about these functions is they take any number of arguments and append them together. For example, we can write a paste function that takes your height in inches and prints a human-readable string.
<- 70
height_in_inches paste0("My height is ", height_in_inches %/% 12, "'", height_in_inches %% 12, "\"")
[1] "My height is 5'10\""
Note that numbers get automatically converted to a string. One subtle point you might have missed. If we start and end strings with double quotes, how can we include one in the text itself? Above, we did this with the escape key \"
. You probably won’t need to do this, but it’s worth mentioning nevertheless.
mean_baby_weight
to make the code nicer to read. These were the weights: (7.7, 8.2, 8.3, 7.6, 9.2, 7.4, 11.1)
\U1F919
. What is displayed?