Little arithmetics with R
In its most basic form R can be used as a simple calculator. Consider the following arithmetic operators:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Exponentiation:
^
- Modulo:
%%
The last two might need some explaining:
- The
^
operator raises the number to its left to the power of the number to its right: for example3^2
equals 9. - The modulo returns the remainder of the division of the number to the left by the number on its right, for example 5 modulo 3 or
5 %% 3
equals 2.
# Addition
5 + 5
# Subtraction
5 - 5
# Multiplication
3 * 5
# Division
(5 + 5) / 2
# Exponentiation
#calculate 2 to the power 5.
2^5
# Modulo
# calculate 28 modulo 6.
28 %% 6
R's pros and cons
As Filip explained in the video, there are things that make R the awesome and immensely popular language that it is today. On the other hand, there are also aspects about R that are less attractive. Which of the following statements are true regarding this statistical programming language developed by Ihaka and Gentleman in the nineties?
- As opposed to SAS and SPSS, R is completely open-source. √
- R is open-source, but it's hard to share your code with others since R uses a command-line interface.
- It typically takes a long time for new and updated R packages to be released and made available to the public.
- R is easy to use, but this comes at the cost of limited graphical abilities.
- R works well with large data sets, if the code is properly written and the data fits into the working memory.√
Variable assignment
A basic concept in R programming is the variable. It allows you to store a value or an object in R. You can then later use this variable's name to easily access the value or the object that is stored within this variable. You use <-
to assign a variable:
my_variable <- 4
# Assign the value 42 to x
x <- 42
# Print out the value of the variable x
x
Variable assignment (2)
Suppose you have a fruit basket with five apples. As a data analyst in training, you want to store the number of apples in a variable with the name my_apples
.
# Assign the value 5 to the variable called my_apples
my_apples <- 5
# Print out the value of the variable my_apples
my_apples
Variable assignment (3)
Every tasty fruit basket needs oranges, so you decide to add six oranges. As a data analyst, your reflex is to immediately create the variable my_oranges
and assign the value 6 to it. Next, you want to calculate how many pieces of fruit you have in total. Since you have given meaningful names to these values, you can now code this in a clear way:
my_apples + my_oranges
# Assign a value to the variables my_apples and my_oranges
my_apples <- 5
my_oranges <- 6
# Add these two variables together and print the result
my_fruit <- my_apples + my_oranges
# Create the variable my_fruit
my_fruit
The workspace
If you assign a value to a variable, this variable is stored in the workspace. It's the place where all user-defined variables and objects live. The command ls()
lists the contents of this workspace. rm(<var_name>)
allows you to remove objects from the workspace again. Try the following code in the console:
a <- 1
b <- 2
ls()
rm(a)
ls()
The first two lines create the variables a
and b
. Calling ls()
now shows you that a
and b
are in the workspace. After removing a
using rm(a)
, the same ls()
command will show you that only b
remains in the workspace. You could also remove both a
and b
in a one-liner: rm(a,b)
.
The first line of the sample code is rm(list = ls())
. This is a very useful command to clear everything from your workspace!
# Clear the entire workspace
rm(list = ls())
# List the contents of your workspace
ls()
# Create the variable horses
horses <- 3
# Create the variable dogs
dogs <- 7
# Create the variable animals
animals <- horses + dogs
# Inspect the contents of the workspace again
ls()
# Remove dogs from the workspace
rm(dogs)
# Inspect the objects in your workspace once more
ls()
Build and destroy your workspace
Apples and oranges, dogs and horses, you can model practically everything in R. The only limit is your own imagination! However, how you create and manage the variables you're creating is always the same. If fruits are not your kind of thing, you're in luck! In this final coding exercise, you will compute the volume of a donut. The volume of a donut can be expressed as:
where rr is the minor radius and RR is the major radius. This is the same as computing the area of the cylindrical portion of the donut (πr2πr2) and multiplying it by the circumference of the donut (2πR2πR). Top off this theory with some workspace management and you've got one tasty challenge! One last tip: ππ is available in R by default as pi
.
# Create the variables r and R
r <- 2
R <- 6
# Calculate the volume of the donut: vol_donut
vol_donut <- 2 * pi^2 * 2^2 * 6
# Remove all intermediary variables that you've used with rm()
rm(r,R)
# List the elements in your workspace
ls()