Learn to R blog series - Operators and Objects
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!
In R, we have our common operators that you’re probably used to if you’ve performed calculations on computers before.
|Raise to the power||^||
R adheres to BODMAS so you can construct safe calculations that combine operators in reliable ways.
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.
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.
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.
|Less than (lt)||<||
|lt or equal to||<=||
|Greater than (gt)||>||
|gt or equal to||>=||
|Exactly equal||==||2 == 2 is TRUE|
|Not equal||!=||2 != 2 is FALSE|
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.
The ampersand (
&) allows us to combine two checks to do an AND check, which is “are both things true?”.
The pipe, or bar (
|) 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?”.
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.
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.
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!
## 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!
P.S. My video code is below if you want to take it away with you!
3 + 3
##  6
10 - 5
##  5
100 / 4
##  25
25 * 9
##  225
6 ^ 2
##  36
2 + 3 * 10 - 7
##  25
(2 + 3) * 10 - 7
##  43
3 < 5
##  TRUE
6 > 4
##  TRUE
3.5 == 3.5
##  TRUE
9 <= 12
##  TRUE
12 != 9
##  TRUE
24 >= 27
##  FALSE
 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
 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.