Introduction to R Basics
Take the first steps with R and learn how to use R as a calculator, how to assign variables, and get to know the basic data types in R.
How it Works
Using the R editor, every line of code is interpreted and executed by R when you select run all. The output of your R code is show in the console.
Note that you can also execute R commands straight in the console. This is a good way to experiment with your code first.
R makes use of the #
sign to add comments, so that you and others can understand what the R code is about. Comments are not run as R code, so they will not influence your result. For example, # Calculate 3 + 4
.
See how the console shows the result of the R code run from R. Now that your familiar with the interface, let’s get down to business.
Arithmetic with R
In its most basic form, R can be used as a simple calculator. Here are the basic arithmetic operators:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Exponentiation:
^
- Modulo:
%%
The last two might need some explaining:
- The
^
operator raises the number to its left to the power of of the number to its right: for example3^2
is 9. - The modulo
%%
operator returns the remainder of the division of the number to the left by the number on its right, for example5%%3
is 2.
For you to try
Try to write some R commands using the arithmetic operations you just learned: addition, subtraction, multiplication, division, exponentiation, and modulo.
Solution
Variable Assignment
A basic concept in (statistical) programming is called a variable.
A variable allows you to store a value (e.g. 4) or an object (e.g. a function description) 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.
For example, you can assign a value of 4 to the variable x
with the command x <- 4
.
For you to try
Assign the value 42 to the variable x
. Then print out the value of the variable x
.
Solution
For you to try (2)
Now, suppose you have a fruit basket with five apples. Create a new variable to store the number of apples. (Hint: you can use a variable with the name my_apples
).
Solution
For you to try (3)
Every tasty fruit basket needs oranges, so you decide to add six oranges. Create a variable and assign the number of oranges to it. Then, calculate the total pieces of fruit you have.
Solution
Apples and Oranges
Common knowledge tells you not to add apples and oranges. In the previous exercise, both the my_apples
and my_oranges
variables contained a number. The +
operator works with numeric variables in R. However, if you really tried to add “apples” and “oranges”, and assigned a text value to the variable my_oranges
, you would be trying to assign the addition of a numeric and a character variable to the variable. This is not possible.
For you to try
Adjust the code so the R knows you have 6 oranges and thus a fruit basket with 11 pieces of fruit. Here is the code for you to copy and modify:
# Assign a value to the variable my_apples
my_apples <- 5# Fix the assignment of my_oranges
my_oranges <- "six"# Create the variable my_fruit and print it out
my_fruit <- my_apples + my_oranges
my_fruit
Solution
Basic Data Types in R
R works with numerous data types. Some of the most basic types are:
- Decimal values like
4.5
are called numerics. - Natural numbers like
4
are called integers. Integers are also numerics. - Boolean values (
TRUE
orFALSE
) are called logical. - Text (or string) values are called characters.
For you to try
Change the value of the following variables such that:
my_numeric
equals42
.my_character
is now"universe"
. Note that the quotation marks indicate that"universe"
is a character.my_logical
becomesFALSE
Note that R is case sensitive.
# Change my_numeric to be 42
my_numeric <- 42.5# Change my_character to be "universe"
my_character <- "some text"# Change my_logical to be FALSE
my_logical <- TRUE
Solution
What’s that Data Type?
Recall how when you added 5 + "six"
, you got an error due to a mismatch in datatypes. You can avoid such situations by checking the data type of a variable beforehand. To do this, use the class()
function.
For you to try
Given the following variables, determine what their data type is.
# Declare variables of different types
my_numeric <- 42
my_character <- "universe"
my_logical <- FALSE