Learn R (Full Tutorial)


Rย Introduction


What is R

R is a popular programming language used for statistical computing and graphical presentation.

Its most common use is to analyze and visualize data.


Why Use R?

  • It is a great resource for data analysis, data visualization, data science and machine learning
  • It provides many statistical techniques (such as statistical tests, classification, clustering and data reduction)
  • It is easy to draw graphs in R, like pie charts, histograms, box plot, scatter plot, etc++
  • It works on different platforms (Windows, Mac, Linux)
  • It is open-source and free
  • It has a large community support
  • It has many packages (libraries of functions) that can be used to solve different problems


Rย Get Started


How to Install R

To install R, go toย https://cloud.r-project.org/ย and download the latest version of R for Windows, Mac or Linux.

When you have downloaded and installed R, you can run R on your computer.

The screenshot below shows how it may look like when you run R on a Windows PC:

If you typeย 5 + 5, and press enter, you will see that R outputsย 10.


Learning R at W3Schools

When learning R at W3Schools.com, you can use our “Try it Yourself” tool, which shows both the code and the result in your browser. This will make it easier for you to test and understand every part as we move forward:

Example

5ย +ย 5

Result:

[1] 10


Rย Syntax


Syntax

To output text in R, use single or double quotes:

Example

“Hello World!”

To output numbers, just type the number (without quotes):

Example

5
10
25

To do simple calculations, add numbers together:

Example

5ย +ย 5


Rย Print Output


Print

Unlike many other programming languages, you can output code in R without using a print function:

Example

“Hello World!”

However, R does have aย print()ย function available if you want to use it. This might be useful if you are familiar with other programming languages, such asย Python, which often uses theย print()ย function to output code.

Example

print(“Hello World!”)

And there are times you must use theย print()ย function to output code, for example when working withย forย loops (which you will learn more about in a later chapter):

Example

forย (xย inย 1:10) {
print(x)
}

Conclusion:ย It is up to you whether you want to use theย print()ย function to output code. However, when your code is inside an R expression (e.g. inside curly bracesย {}ย like in the example above), use theย print()ย function to output the result.



Rย Comments


Comments can be used to explain R code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Comments starts with aย #. When executing code, R will ignore anything that starts withย #.

This example uses a comment before a line of code:

Example

# This is a comment
“Hello World!”

This example uses a comment at the end of a line of code:

Example

“Hello World!”ย # This is a comment

Comments does not have to be text to explain the code, it can also be used to prevent R from executing the code:

Example

# “Good morning!”
“Good night!”

Multiline Comments

Unlike other programming languages, such asย Java, there are no syntax in R for multiline comments. However, we can just insert aย #ย for each line to create multiline comments:

Example

# This is a comment
# written in
# more than just one line
“Hello World!”


Rย Variables


Creating Variables in R

Variables are containers for storing data values.

R does not have a command for declaring a variable. A variable is created the moment you first assign a value to it. To assign a value to a variable, use theย <-ย sign. To output (or print) the variable value, just type the variable name:

Example

name <-ย “John”
age <-ย 40
nameย ย ย # output “John”
ageย ย ย ย # output 40

Try it Yourself ยป

From the example above,ย nameย andย ageย areย variables, whileย "John"ย andย 40ย areย values.

In other programming language, it is common to useย =ย as an assignment operator. In R, we can use bothย =ย andย <-ย as assignment operators.

However,ย <-ย is preferred in most cases because theย =ย operator can be forbidden in some context in R.


Print / Output Variables

Compared to many other programming languages, you do not have to use a function to print/output variables in R. You can just type the name of the variable:

Example

name <-ย “John Doe”

nameย # auto-print the value of the name variable

However, R does have aย print()ย function available if you want to use it. This might be useful if you are familiar with other programming languages, such asย Python, which often use aย print()ย function to output variables.

Example

name <-ย “John Doe”

print(name)ย # print the value of the name variable

And there are times you must use theย print()ย function to output code, for example when working withย forย loops (which you will learn more about in a later chapter):

Example

forย (xย inย 1:10) {
print(x)
}

Conclusion:ย It is up to your if you want to use theย print()ย function or not to output code. However, when your code is inside an R expression (for example inside curly bracesย {}ย like in the example above), use theย print()ย function if you want to output the result.


Concatenate Elements

You can also concatenate, or join, two or more elements, by using theย paste()ย function.

To combine both text and a variable, R uses comma (,):

Example

text <-ย “awesome”

paste(“R is”, text)

You can also useย ,ย to add a variable to another variable:

Example

text1 <-ย “R is”
text2 <-ย “awesome”
paste(text1, text2)

For numbers, theย +ย character works as a mathematical operator:

Example

num1 <-ย 5
num2 <-ย 10
num1 + num2

If you try to combine a string (text) and a number, R will give you an error:

Example

num <-ย 5
text <-ย “Some text”
num + text

Result:

Error in num + text : non-numeric argument to binary operator

Multiple Variables

R allows you to assign the same value to multiple variables in one line:

Example

# Assign the same value to multiple variables in one line
var1 <- var2 <-ย var3 <-ย “Orange”
# Print variable values
var1
var2
var3


Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for R variables are:

  • A variable name must start with a letter and can be a combination of letters, digits, period(.)
    and underscore(_). If it starts with period(.), it cannot be followed by a digit.
  • A variable name cannot start with a number or underscore (_)
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • Reserved words cannot be used as variables (TRUE, FALSE, NULL, if…)
# Legal variable names:
myvar <-ย “John”
my_var <-ย “John”
myVarย <-ย “John”
MYVAR <-ย “John”
myvar2 <-ย “John”
.myvar <-ย “John”
# Illegal variable names:
2myvar <-ย “John”
my-var <-ย “John”
my var <-ย “John”
_my_var <-ย “John”
my_v@ar <-ย “John”
TRUE <-ย “John”

Remember that variable names are case-sensitive!



Rย Data Types


In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

In R, variables do not need to be declared with any particular type, and can even change type after they have been set:

Example

my_var <-ย 30ย # my_var is type ofย numeric
my_var <-ย “Sally”ย # my_var is now of typeย character (aka string)

R has a variety of data types and object classes. You will learn much more about these as you continue to get to know R.


Basic Data Types

Basic data types in R can be divided into the following types:

  • numericย – (10.5, 55, 787)
  • integerย – (1L, 55L, 100L, where the letter “L” declares this as an integer)
  • complexย – (9 + 3i, where “i” is the imaginary part)
  • characterย (a.k.a. string) – (“k”, “R is exciting”, “FALSE”, “11.5”)
  • logicalย (a.k.a. boolean) – (TRUE or FALSE)

We can use theย class()ย function to check the data type of a variable:

Example

# numeric
x <-ย 10.5
class(x)
# integer
x <- 1000L
class(x)

# complex
x <- 9i +ย 3
class(x)

# character/string
x <-ย “R is exciting”
class(x)

# logical/boolean
x <- TRUE
class(x)



Rย Numbers


There are three number types in R:

  • numeric
  • integer
  • complex

Variables of number types are created when you assign a value to them:

Example

x <-ย 10.5ย ย ย # numeric
y <- 10Lย ย ย ย # integer
z <- 1iย ย ย ย ย # complex

Numeric

Aย numericย data type is the most common type in R, and contains any number with or without a decimal, like: 10.5, 55, 787:

Example

x <-ย 10.5
y <-ย 55
# Print values of x and y
x
y

# Print the class name of x and y
class(x)
class(y)


Integer

Integers are numeric data without decimals. This is used when you are certain that you will never create a variable that should contain decimals. To create anย integerย variable, you must use the letterย Lย after the integer value:

Example

x <- 1000L
y <- 55L
# Print values of x and y
x
y

# Print the class name of x and y
class(x)
class(y)


Complex

Aย complexย number is written with an “i” as the imaginary part:

Example

x <-ย 3+5i
y <- 5i
# Print values of x and y
x
y

# Print the class name of x and y
class(x)
class(y)


Type Conversion

You can convert from one type to another with the following functions:

  • as.numeric()
  • as.integer()
  • as.complex()

Example

x <- 1Lย # integer
y <-ย 2ย # numeric

# convert from integer to numeric:
a <-ย as.numeric(x)
# convert from numeric to integer:
bย <-ย as.integer(y)

# print values of x and y
x
y

# print the class name of a and b
class(a)
class(b)



Rย Math


Simple Math

In R, you can useย operatorsย to perform common mathematical operations on numbers.

Theย +ย operator is used to add together two values:

Example

10ย +ย 5

And theย -ย operator is used for subtraction:

Example

10ย –ย 5

Built-in Math Functions

R also has many built-in math functions that allows you to perform mathematical tasks on numbers.

For example, theย min()ย andย max()ย functions can be used to find the lowest or highest number in a set:

Example

max(5,ย 10,ย 15)

min(5,ย 10,ย 15)


sqrt()

Theย sqrt()ย function returns the square root of a number:

Example

sqrt(16)

abs()

Theย abs()ย function returns the absolute (positive) value of a number:

Example

abs(-4.7)

ceiling() and floor()

Theย ceiling()ย function rounds a number upwards to its nearest integer, and theย floor()ย function rounds a number downwards to its nearest integer, and returns the result:

Example

ceiling(1.4)

floor(1.4)



Rย Strings


Strings are used for storing text.

A string is surrounded by either single quotation marks, or double quotation marks:

"hello"ย is the same asย 'hello':

Example

“hello”
‘hello’

Assign a String to a Variable

Assigning a string to a variable is done with the variable followed by theย <-ย operator and the string:

Example

strย <-ย “Hello”
strย # print the value of str

Multiline Strings

You can assign a multiline string to a variable like this:

Example

strย <-ย “Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.”
strย # print the value of str

However, note that R will add a “\n” at the end of each line break. This is called an escape character, and theย nย character indicates aย new line.

If you want the line breaks to be inserted at the same position as in the code, use theย cat()ย function:

Example

strย <-ย “Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.”
cat(str)


String Length

There are many usesful string functions in R.

For example, to find the number of characters in a string, use theย nchar()ย function:

Example

strย <-ย “Hello World!”

nchar(str)


Check a String

Use theย grepl()ย function to check if a character or a sequence of characters are present in a string:

Example

strย <-ย “Hello World!”

grepl(“H”,ย str)
grepl(“Hello”,ย str)
grepl(“X”,ย str)


Combine Two Strings

Use theย paste()ย function to merge/concatenate two strings:

Example

str1 <-ย “Hello”
str2 <-ย “World”
paste(str1, str2)


Escape Characters

To insert characters that are illegal in a string, you must use an escape character.

An escape character is a backslashย \ย followed by the character you want to insert.

An example of an illegal character is a double quote inside a string that is surrounded by double quotes:

Example

strย <-ย “We are the so-called “Vikings“, from the north.”

str

Result:

Error: unexpected symbol in "str <- "We are the so-called "Vikings"

To fix this problem, use the escape characterย \":

Example

The escape character allows you to use double quotes when you normally would not be allowed:

strย <-ย “We are the so-called \”Vikings\“, from the north.”

str
cat(str)

Note that auto-printing theย strย variable will print the backslash in the output. You can use theย cat()ย function to print it without backslash.

Other escape characters in R:

Code Result
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace


Rย Booleans / Logical Values


Booleans (Logical Values)

In programming, you often need to know if an expression isย trueย orย false.

You can evaluate any expression in R, and get one of two answers,ย TRUEย orย FALSE.

When you compare two values, the expression is evaluated and R returns the logical answer:

Example

10ย >ย 9ย ย ย ย # TRUE because 10 is greater than 9
10ย ==ย 9ย ย ย # FALSE because 10 is not equal to 9
10ย <ย 9ย ย ย ย # FALSE because 10 is greater than 9

You can also compare two variables:

Example

a <-ย 10
b <-ย 9
a > b

You can also run a condition in anย if statement.

Example

a <-ย 200
b <-ย 33
ifย (b > a) {
printย (“b is greater than a”)
}ย elseย {
print(“b is not greater than a”)
}



Rย Operators


Operators are used to perform operations on variables and values.

In the example below, we use theย +ย operator to add together two values:

Example

10ย +ย 5

R divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Miscellaneous operators

R Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Example
+ Addition x + y
Subtraction x – y
* Multiplication x * y
/ Division x / y
^ Exponent x ^ y
%% Modulus (Remainder from division) x %% y
%/% Integer Division x%/%y

R Assignment Operators

Assignment operators are used to assign values to variables:

Example

my_var <-ย 3

my_var <<-ย 3

3ย -> my_var

3ย ->>ย my_var

my_varย # print my_var

Note:ย <<- is a global assigner. It is also possible to turn the direction of the assignment operator. x <- 3 is equal to 3 -> x


R Comparison Operators

Comparison operators are used to compare two values:

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

R Logical Operators

Logical operators are used to combine conditional statements:

Operator Description
& Element-wise Logical AND operator. It returns TRUE if both elements are TRUE
&& Logical AND operator – Returns TRUE if both statements are TRUE
| Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE
|| Logical OR operator. It returns TRUE if one of the statement is TRUE.
! Logical NOT – returns FALSE if statement is TRUE

R Miscellaneous Operators

Miscellaneous operators are used to manipulate data:

Operator Description Example
: Creates a series of numbers in a sequence x <- 1:10
%in% Find out if an element belongs to a vector x %in% y
%*% Matrix Multiplication x <- Matrix1 %*% Matrix2


Rย If … Else


Conditions and If Statements

R supports the usual logical conditions from mathematics:

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

These conditions can be used in several ways, most commonly in “if statements” and loops.


The if Statement

An “if statement” is written with theย ifย keyword, and it is used to specify a block of code to be executed if a condition isย TRUE:

Example

a <-ย 33
b <-ย 200
ifย (b > a) {
print(“b is greater than a”)
}

In this example we use two variables,ย aย andย b, which are used as a part of the if statement to test whetherย bย is greater thanย a. Asย aย isย 33, andย bย isย 200, we know that 200 is greater than 33, and so we print to screen that “b is greater than a”.

R uses curly brackets { } to define the scope in the code.


Else If

Theย else ifย keyword is R’s way of saying “if the previous conditions were not true, then try this condition”:

Example

a <-ย 33
b <-ย 33
ifย (b > a) {
print(“b is greater than a”)
}ย elseย ifย (a == b) {
printย (“a and b are equal”)
}

In this exampleย aย is equal toย b, so the first condition is not true, but theย else ifย condition is true, so we print to screen that “a and b are equal”.

You can use as manyย else ifย statements as you want in R.


If Else

Theย elseย keyword catches anything which isn’t caught by the preceding conditions:

Example

a <-ย 200
b <-ย 33
ifย (b > a) {
print(“b is greater than a”)
}ย elseย ifย (a == b) {
print(“a and b are equal”)
}ย elseย {
print(“a is greater than b”)
}

In this example,ย aย is greater thanย b, so the first condition is not true, also theย else ifย condition is not true, so we go to theย elseย condition and print to screen that “a is greater than b”.

You can also useย elseย withoutย else if:

Example

a <-ย 200
b <-ย 33
ifย (b > a) {
print(“b is greater than a”)
}ย elseย {
print(“b is not greater than a”)
}


Nested If Statements

You can also haveย ifย statements insideย ifย statements, this is calledย nestedย ifย statements.

Example

x <-ย 41

ifย (x >ย 10) {
print(“Above ten”)
ifย (x >ย 20) {
print(“and also above 20!”)
}ย elseย {
print(“but not above 20.”)
}
}ย elseย {
print(“below 10.”)
}


AND

Theย &ย symbol (and) is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b, AND if c is greater than a:

a <-ย 200
b <-ย 33
c <-ย 500
ifย (a > b & c > a) {
print(“Both conditions are true”)
}


OR

Theย |ย symbol (or) is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b, or if c is greater than a:

a <-ย 200
b <-ย 33
c <-ย 500
ifย (a > b | a > c) {
print(“At least one of the conditions is true”)
}



Rย While Loop


Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.

R has two loop commands:

  • whileย loops
  • forย loops

R While Loops

With theย whileย loop we can execute a set of statements as long as a condition is TRUE:

Example

Printย iย as long asย iย is less than 6:

i <-ย 1
whileย (i <ย 6) {
print(i)
i <- i +ย 1
}

In the example above, the loop will continue to produce numbers ranging from 1 to 5. The loop will stop at 6 becauseย 6 < 6ย is FALSE.

Theย whileย loop requires relevant variables to be ready, in this example we need to define an indexing variable,ย i, which we set to 1.

Note:ย remember to increment i, or else the loop will continue forever.


Break

With theย breakย statement, we can stop the loop even if the while condition is TRUE:

Example

Exit the loop ifย iย is equal to 4.

i <-ย 1
whileย (i <ย 6) {
print(i)
i <- i +ย 1
ifย (i ==ย 4) {
break
}
}

The loop will stop at 3 because we have chosen to finish the loop by using theย breakย statement whenย iย is equal to 4 (i == 4).


Next

With theย nextย statement, we can skip an iteration without terminating the loop:

Example

Skip the value of 3:

i <-ย 0
whileย (i <ย 6) {
i <- i +ย 1
ifย (i ==ย 3) {
next
}
print(i)
}

When the loop passes the value 3, it will skip it and continue to loop.


Yahtzee!

If .. Else Combined with a While Loop

To demonstrate a practical example, let us say we play a game of Yahtzee!

Example

Print “Yahtzee!” If the dice number is 6:

dice <-ย 1
whileย (dice <=ย 6) {
ifย (dice <ย 6) {
print(“No Yahtzee”)
}ย elseย {
print(“Yahtzee!”)
}
dice <- diceย +ย 1
}

If the loop passes the values ranging from 1 to 5, it prints “No Yahtzee”. Whenever it passes the value 6, it prints “Yahtzee!”.



Rย For Loop


For Loops

Aย forย loop is used for iterating over a sequence:

Example

forย (xย inย 1:10) {
print(x)
}

This is less like theย forย keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

With theย for loop we can execute a set of statements, once for each item in a vector, array, list, etc..

Example

Print every item in a list:

fruits <-ย list(“apple”,ย “banana”,ย “cherry”)

forย (xย inย fruits) {
print(x)
}

Example

Print the number of dices:

dice <- c(1,ย 2,ย 3,ย 4,ย 5,ย 6)

forย (xย inย dice) {
print(x)
}

Theย forย loop does not require an indexing variable to set beforehand, like withย whileย loops.


Break

With theย breakย statement, we can stop the loop before it has looped through all the items:

Example

Stop the loop at “cherry”:

fruits <-ย list(“apple”,ย “banana”,ย “cherry”)

forย (xย inย fruits) {
ifย (x ==ย “cherry”) {
break
}
print(x)
}

The loop will stop at “cherry” because we have chosen to finish the loop by using theย breakย statement whenย xย is equal to “cherry” (x == "cherry").


Next

With theย nextย statement, we can skip an iteration without terminating the loop:

Example

Skip “banana”:

fruits <-ย list(“apple”,ย “banana”,ย “cherry”)

forย (xย inย fruits) {
ifย (x ==ย “banana”) {
next
}
print(x)
}

When the loop passes “banana”, it will skip it and continue to loop.


Yahtzee!

If .. Else Combined with a For Loop

To demonstrate a practical example, let us say we play a game of Yahtzee!

Example

Print “Yahtzee!” If the dice number is 6:

dice <-ย 1:6

for(xย inย dice) {
ifย (x ==ย 6) {
print(paste(“The dice number is”, x,ย “Yahtzee!”))
}ย elseย {
print(paste(“The dice number is”, x,ย “Not Yahtzee”))
}
}

If the loop reaches the values ranging from 1 to 5, it prints “No Yahtzee” and its number. When it reaches the value 6, it prints “Yahtzee!” and its number.


Nested Loops

It is also possible to place a loop inside another loop. This is called aย nested loop:

Example

Print the adjective of each fruit in a list:

adj <-ย list(“red”,ย “big”,ย “tasty”)

fruits <-ย list(“apple”,ย “banana”,ย “cherry”)
forย (xย inย adj) {
forย (yย inย fruits) {
print(paste(x, y))
}
}



Rย Functions


A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.


Creating a Function

To create a function, use theย function()ย keyword:

Example

my_function <- function() {ย # create a function with the name my_function
ย ย print(“Hello World!”)
}

Call a Function

To call a function, use the function name followed by parenthesis, likeย my_function():

Example

my_function <- function() {
print(“Hello World!”)
}
my_function()ย # call the function named my_function


Arguments

Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:

Example

my_function <- function(fname) {
paste(fname,ย “Griffin”)
}
my_function(“Peter”)
my_function(“Lois”)
my_function(“Stewie”)

Parameters or Arguments?

The terms “parameter” and “argument” can be used for the same thing: information that are passed into a function.

From a function’s perspective:

A parameter is the variable listed inside the parentheses in the function definition.

An argument is the value that is sent to the function when it is called.


Number of Arguments

By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less:

Example

This function expects 2 arguments, and gets 2 arguments:

my_function <- function(fname, lname) {
paste(fname, lname)
}
my_function(“Peter”,ย “Griffin”)

If you try to call the function with 1 or 3 arguments, you will get an error:

Example

This function expects 2 arguments, and gets 1 argument:

my_function <- function(fname, lname) {
paste(fname, lname)
}
my_function(“Peter”)


Default Parameter Value

The following example shows how to use a default parameter value.

If we call the function without an argument, it uses the default value:

Example

my_function <- function(country =ย “Norway”) {
paste(“I am from”, country)
}
my_function(“Sweden”)
my_function(“India”)
my_function()ย # will get the default value, which is Norway
my_function(“USA”)


Return Values

To let a function return a result, use theย return()ย function:

Example

my_function <- function(x) {
returnย (5ย * x)
}
print(my_function(3))
print(my_function(5))
print(my_function(9))

The output of the code above will be:

[1] 15
[1] 25
[1] 45


Nested Functions

There are two ways to create a nested function:

  • Call a function within another function.
  • Write a function within a function.

Example

Call a function within another function:

Nested_function <- function(x, y) {
a <- x + y
return(a)
}
Nested_function(Nested_function(2,2), Nested_function(3,3))

Example Explained

The function tells x to add y.

The first input Nested_function(2,2) is “x” of the main function.

The second input Nested_function(3,3) is “y” of the main function.

The output is therefore (2+2) + (3+3) =ย 10.

Example

Write a function within a function:

Outer_func <- function(x) {
Inner_func <- function(y) {
a <- x + y
return(a)
}
returnย (Inner_func)
}
output <- Outer_func(3)ย # To call the Outer_func
output(5)

Example Explained

You cannot directly call the function because the Inner_func has been defined (nested) inside the Outer_func.

We need to call Outer_func first in order to call Inner_func as a second step.

We need to create a new variable called output and give it a value, which is 3 here.

We then print the output with the desired value of “y”, which in this case is 5.

The output is thereforeย 8ย (3 + 5).


Recursion

R also accepts function recursion, which means a defined function can call itself.

Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.

The developer should be very careful with recursion as it can be quite easy to slip into writing a function which never terminates, or one that uses excess amounts of memory or processor power. However, when written correctly, recursion can be a very efficient and mathematically-elegant approach to programming.

In this example,ย tri_recursion()ย is a function that we have defined to call itself (“recurse”). We use theย kย variable as the data, which decrements (-1) every time we recurse. The recursion ends when the condition is not greater than 0 (i.e. when it is 0).

To a new developer it can take some time to work out how exactly this works, best way to find out is by testing and modifying it.

Example

tri_recursion <- function(k) {
ifย (k >ย 0) {
result <- k + tri_recursion(k –ย 1)
print(result)
}ย elseย {
result =ย 0
return(result)
}
}
tri_recursion(6)

Global Variables

Variables that are created outside of a function are known asย globalย variables.

Global variables can be used by everyone, both inside of functions and outside.

Example

Create a variable outside of a function and use it inside the function:

txt <-ย “awesome”
my_function <- function() {
paste(“R is”,ย txt)
}
my_function()

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

Example

Create a variable inside of a function with the same name as the global variable:

txt <-ย “global variable”
my_function <- function() {
txt =ย “fantastic”
paste(“R is”, txt)
}
my_function()

txtย # print txt

If you try to printย txt, it will return “global variable” because we are printingย txtย outside the function.


The Global Assignment Operator

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use theย global assignmentย operatorย <<-

Example

If you use the assignment operatorย <<-, the variable belongs to the global scope:

my_function <- function() {
txt <<-ย “fantastic”
paste(“R is”,ย txt)
}
my_function()

print(txt)

Also, use theย globalย assignment operator if you want to change a global variable inside a function:

Example

To change the value of a global variable inside a function, refer to the variable by using the global assignment operatorย <<-:

txt <-ย “awesome”
my_function <- function() {
txt <<-ย “fantastic”
paste(“R is”, txt)
}
my_function()

paste(“R is”,ย txt)