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.

Linda Ngo
The Startup
5 min readMay 21, 2020

--

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.

Source: R for Spatial Statistics

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 example 3^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 example 5%%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

R Script
Output

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

We assign a value to a variable using: variable <- value
Notice that when you ask R to print the variable, the value of the variable appears.

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

So R now links the variable my_apples to the value 5.

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.

Notice that you will get an error message if to try to do this.

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:

Solution

Modified code.
There will no longer be an error since you are performing arithmetic with two numeric values now.

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 or FALSE ) 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 equals 42 .
  • my_character is now "universe" . Note that the quotation marks indicate that "universe" is a character.
  • my_logical becomes FALSE

Note that R is case sensitive.

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.

Solution

Use the class() function to check.

--

--

Linda Ngo
The Startup

Currently pursuing a degree in Computer Science. Passionate about learning new things.