Learn to R blog series - Operators and Objects
Basic operations
Now that we have some datatypes, we can start learning what we can do with them.
This weeks video whisks over the basic operators - you know what plus and minus do, right? Then we look at some other less common operators and recap it all below.
Pay special attention to all.equal()
, there’s a reason I bang on about it!
Maths
In R, we have our common operators that you’re probably used to if you’ve performed calculations on computers before.
Action | Operator | Example |
---|---|---|
Subtract | - | 5 - 4 = 1 |
Add | + | 5 + 4 = 9 |
Multiply | * | 5 * 4 = 20 |
Divide | / | 5 / 4 = 1.25 |
Raise to the power | ^ | 5 ^ 4 = 625 |
R adheres to BODMAS[1] so you can construct safe calculations that combine operators in reliable ways.
Action | Operator | Example |
---|---|---|
Basic sequence | : | 1:3 = 1, 2, 3 |
Integer division | %/% | 9 %/% 4 = 2 |
Modulus | %% | 9 %% 4 = 1 |
The colon (:
) is a snazzy way of generating a sequence of numbers that step by 1. You specify a beginning number and an end number and R will produce all the whole numbers including and between the two numbers. This even works for negative numbers or producing descending values.
Comparison
The next important thing to know about is how to write comparisons; ways of looking at two or more things and finding out if they’re the same, or different.
Common operators
The less thans and greater thans are symbols that are in pretty much every language for comparisons, but the test to see if two values are the same or not can often vary across languages.
Summary
Action | Operator | Example |
---|---|---|
Less than (lt) | < | 5 < 5 = FALSE |
lt or equal to | <= | 5 <= 5 = TRUE |
Greater than (gt) | > | 5 > 5 = FALSE |
gt or equal to | >= | 5 >= 5 = TRUE |
Exactly equal | == | (0.5 - 0.3) == (0.3 - 0.1) is FALSE |
Exactly equal | == | 2 == 2 is TRUE |
Not equal | != | (0.5 - 0.3) != (0.3 - 0.1) is TRUE |
Not equal | != | 2 != 2 is FALSE |
Equal | all.equal() | all.equal(0.5-0.3,0.3-0.1) is TRUE |
In | %in% | "Red" %in% c("Blue","Red") is TRUE |
Logic
Once we can do a single check, we inevitably want to do multiple checks at the same time.
To combine multiple checks, we can use logical operators.
Common operators
The ampersand (&
) allows us to combine two checks to do an AND check, which is “are both things true?”.
The pipe, or bar (|
)[2] allows us to do an OR check, which is “are either of these things true?”.
The exclamation point (!
) allows us to a perform a NOT check, by negating or swapping a check’s result. This allows you say things like “is this check true and that check not true?”.
Other operators
Less commonly, there other logical checks you might to perform.
We can do an XOR, where one and only one of two values being checked is true.
Summary
We can produce sophisticated checks from a few simple building blocks. This will come in very handy down the line when doing things like filtering datasets or creating new fields in your data.
Action | Operator | Example |
---|---|---|
Not | ! | !TRUE is FALSE |
And | & | TRUE & FALSE is FALSE |
And | & | c(TRUE,TRUE) & c(FALSE,TRUE) is FALSE, TRUE |
Or | ` | ` |
Xor | xor() | xor(TRUE,FALSE) is TRUE |
Play time!
This basic operations section has hopefully taught you how to manipulate values and construct comparisons. These are important building blocks in data analysis, and whilst we’ve been working with only a single value at a time, in the next section we’ll see how it works with more data.
Now take the code below and run wild…or let your turtle run wild at least! Use what you’ve learned here and see what you can do with operators - I’ve given you some blog posts and guides with example code in - take this and alter it so that the numbers are sums, or even better come up with your own!
Four simple turtle graphs.
A guide to the TurtleGraphic package for R This one has some great tricky examples at the end!
install.packages("TurtleGraphics")
library(TurtleGraphics)
## Loading required package: grid
turtle_init() # This starts off the turtle
turtle_down() # This means that when you feed instructions to your turtle, it leaves a mark.
Use “turtle_left()” and place the number of an angle between the brackets to turn your turtle. The same applies for “turtle_right()”
Use “turtle_forward()” and “turtle_backward()” to move him around and draw something. Inbetween the brackets should be a number specifying the number of “steps” the turtle should take.
Here’s the twist. Use what you’ve learned today with the basic maths operations and create your drawing using sums instead of numbers. We’d love to see your efforts so tweet us a picture to @LockeData!
Ellen :)
P.S. My video code is below if you want to take it away with you!
3 + 3
## [1] 6
10 - 5
## [1] 5
100 / 4
## [1] 25
25 * 9
## [1] 225
6 ^ 2
## [1] 36
2 + 3 * 10 - 7
## [1] 25
(2 + 3) * 10 - 7
## [1] 43
3 < 5
## [1] TRUE
6 > 4
## [1] TRUE
3.5 == 3.5
## [1] TRUE
9 <= 12
## [1] TRUE
12 != 9
## [1] TRUE
24 >= 27
## [1] FALSE
[1] Brackets, Other, Division, Multiplication, Addition, Subtraction. Note that in some countries it’s BEDMAS, where the E stands for Exponents, which is a special Other
[2] Getting this symbol can be painful as it varies substantially by keyboard, so apologies if it takes you a while to hunt this symbol down.