UNIT1 INTRODUCTION TO R PROGRAMMING
UNIT1 INTRODUCTION TO R PROGRAMMING
R Programming Language
What is R Programming Language?
R programming is a leading tool for machine learning, statistics, and
data analysis, allowing for the easy creation of objects, functions,
and packages. Designed by Ross Ihaka and Robert Gentleman at the
University of Auckland and developed by the R Development Core
Team, R Language is platform-independent and open-source,
making it accessible for use across all operating systems without
licensing costs. Beyond its capabilities as a statistical package, R
integrates with other languages like C and C++, facilitating
interaction with various data sources and statistical tools. With a
growing community of users and high demand in the Data Science
job market, R is one of the most sought-after programming
languages today. Originating as an implementation of the S
programming language with influences from Scheme, R has evolved
since its conception in 1992, with its first stable beta version
released in 2000.
R vs Python
R Programming Language and Python are both used extensively
for Data Science. Both are very useful and open-source languages
as well. For data analysis, statistical computing, and machine
learning Both languages are strong tools with sizable communities
and huge libraries for data science jobs. A theoretical comparison
between R and Python is provided below:
R vs Python
R is a language and
Python is a general-purpose
environment for statistical
programming language for
Introduction programming which
data analysis and scientific
includes statistical
computing
computing and graphics.
R vs Python
Features R Python
It supports Tidyverse,
You can use NumPy,
making it easy to import,
Data modeling SciPy, scikit-learn, TansorFl
manipulate, visualize, and
ow
report on data.
Statsmodels (OLS)
Ordinary Least Squares
Linear Regression lm() function and Formulas
(OLS) Method
Generalized Linear
glm() function Statsmodels (GLM)
Models (GLM)
Capability R Python
Principal Component
princomp() function scikit-learn (PCA)
Analysis (PCA)
scikit-learn
Decision Trees rpart() function
(DecisionTreeClassifier)
scikit-learn
Random Forest randomForest() function
(RandomForestClassifier)
Primary users are Scholar and R&D Primary users are Programmers and
R Programming Python Programming
developers
Support packages like tidyverse, ggplot2, Support packages like pandas, scipy, scikit-
caret, zoo learn, TensorFlow, caret
R might not be as fast as languages like Python might not be as specialized for
Python, especially for computationally statistics and data analysis as R. Some
intensive tasks and large-scale data statistical functions and visualization
processing. capabilities might be more streamlined in R.
Example
"Hello World!"
Example
5
10
25
Example
5 + 5
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!")
nd 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)
}
R Comments
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 #.
Example
# This is a comment
"Hello World!"
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 CommentsUnlike 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
However, <- is preferred in most cases because the = operator can be forbidden
in some contexts in R.
Example
name <- "John Doe"
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"
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)
}
R Concatenate Elements
Concatenate Elements
You can also concatenate, or join, two or more elements, by using
the paste() function.
Example
text <- "awesome"
Example
text1 <- "R is"
text2 <- "awesome"
paste(text1, text2)
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:
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
R Variable Names
(Identifiers)
❮ PreviousNext ❯
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:
R Data Types
❮ PreviousNext ❯
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)
Try it Yourself »
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.
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
❮ PreviousNext ❯
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
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
Complex
A complex number is written with an "i" as the imaginary part:
Example
x <- 3+5i
y <- 5i
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
Example
10 + 5
Example
10 - 5
You will learn more about available operators in our R Operators Tutorial.
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)
Example
sqrt(16)
abs()
The abs() function returns the absolute (positive) value of a number:
Example
abs(-4.7)
Example
ceiling(1.4)
floor(1.4)
R Strings
String Literals
Strings are used for storing text.
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."
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.
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)
Example
str1 <- "Hello"
str2 <- "World"
paste(str1, str2)
R Escape Characters
Escape Characters
To insert characters that are illegal in a string, you must use an escape
character.
Example
str <- "We are the so-called "Vikings", from the north."
str
Result:
Error: unexpected symbol in "str <- "We are the so-called "Vikings"
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.
Code Result
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
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
a > b
You can also run a condition in an if statement, which you will learn much more
about in the if..else chapter.
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
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
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Miscellaneous operators
R Arithmetic Operators
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
^ Exponent x^y
%% Modulus x %% y
(Remainder
from division)
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
R Comparison Operators
Comparison operators are used to compare two values:
== Equal x == y
!= Not equal x != y
R Logical Operators
Logical operators are used to combine conditional statements:
Operator Description
R Miscellaneous Operators
Miscellaneous operators are used to manipulate data:
R If ... Else
Conditions and If Statements
R supports the usual logical conditions from mathematics:
== Equal x == y
!= Not equal 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".
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".
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".
Example
a <- 200
b <- 33
if (b > a) {
print("b is greater than a")
} else {
print("b is not greater than a")
}
R Nested If
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.")
}
R - AND OR Operators
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) {
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 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.
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.
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-oriented 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:
for (x in fruits) {
print(x)
}
Example
Print the number of dices:
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":
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":
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:
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.
R Nested Loops
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:
R Functions
A function is a block of code which only runs when it is called.
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!")
}
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.
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:
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("Peter")
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))
[1] 15
[1] 25
[1] 45
R Nested Functions
Nested Functions
There are two ways to create a nested function:
Example
Call a function within another function:
Nested_function(Nested_function(2,2), Nested_function(3,3))
Example Explained
Example
Write a function within a function:
Example Explained
You cannot directly call the function because the Inner_func has been defined
(nested) inside the Outer_func.
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.
R Function Recursion
Recursion
R also accepts function recursion, which means a defined function can call itself.
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)
R Global Variables
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:
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:
my_function()
If you try to print txt, it will return "global variable" because we are
printing txt outside the 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:
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 <<-:
my_function()
To combine the list of items to a vector, use the c() function and separate the
items by a comma.
In the example below, we create a vector variable called fruits, that combine
strings:
Example
# Vector of strings
fruits <- c("banana", "apple", "orange")
# Print fruits
fruits
Example
# Vector of numerical values
numbers <- c(1, 2, 3)
# Print numbers
numbers
Example
# Vector with numerical values in a sequence
numbers <- 1:10
numbers
You can also create numerical values with decimals in a sequence, but note that
if the last element does not belong to the sequence, it is not used:
Example
# Vector with numerical decimals in a sequence
numbers1 <- 1.5:6.5
numbers1
Result:
Example
# Vector of logical values
log_values <- c(TRUE, FALSE, TRUE, FALSE)
log_values
Vector Length
To find out how many items a vector has, use the length() function:
Example
fruits <- c("banana", "apple", "orange")
length(fruits)
Sort a Vector
To sort items in a vector alphabetically or numerically, use the sort() function:
Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)
Access Vectors
You can access the vector items by referring to its index number inside
brackets []. The first item has index 1, the second item has index 2, and so on:
Example
fruits <- c("banana", "apple", "orange")
You can also access multiple elements by referring to different index positions
with the c() function:
Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")
You can also use negative index numbers to access all items except the ones
specified:
Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")
Change an Item
To change the value of a specific item, refer to the index number:
Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")
# Print fruits
fruits
Repeat Vectors
To repeat vectors, use the rep() function:
Example
Repeat each value:
repeat_each
Example
Repeat the sequence of the vector:
repeat_times
Example
Repeat each value independently:
repeat_indepent
Generating Sequenced Vectors
One of the examples on top, showed you how to create a vector with numerical
values in a sequence with the : operator:
Example
numbers <- 1:10
numbers
Example
numbers <- seq(from = 0, to = 100, by = 20)
numbers
Note: The seq() function has three parameters: from is where the sequence
starts, to is where the sequence stops, and by is the interval of the sequence.
R Lists
Lists
A list in R can contain many different data types inside it. A list is a collection of
data which is ordered and changeable.
Example
# List of strings
thislist <- list("apple", "banana", "cherry")
Example
thislist <- list("apple", "banana", "cherry")
thislist[1]
Example
thislist <- list("apple", "banana", "cherry")
thislist[1] <- "blackcurrant"
List Length
To find out how many items a list has, use the length() function:
Example
thislist <- list("apple", "banana", "cherry")
length(thislist)
Example
Check if "apple" is present in the list:
Example
Add "orange" to the list:
append(thislist, "orange")
To add an item to the right of a specified index, add " after=index number" in
the append() function:
Example
Add "orange" to the list after "banana" (index 2):
Range of Indexes
You can specify a range of indexes by specifying where to start and where to
end the range, by using the : operator:
Example
Return the second, third, fourth and fifth item:
thislist <-
list("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango
")
(thislist)[2:5]
Note: The search will start at index 2 (included) and end at index 5 (included).
Example
Print all items in the list, one by one:
for (x in thislist) {
print(x)
}
The most common way is to use the c() function, which combines two elements
together:
Example
list1 <- list("a", "b", "c")
list2 <- list(1,2,3)
list3 <- c(list1,list2)
list3
R Matrices
Matrices
A matrix is a two dimensional data set with columns and rows.
A column is a vertical representation of data, while a row is a horizontal
representation of data.
Example
# Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow
= 2, ncol = 2)
thismatrix
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow
= 2, ncol = 2)
thismatrix[1, 2]
The whole row can be accessed if you specify a comma after the number in the
bracket:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow
= 2, ncol = 2)
thismatrix[2,]
The whole column can be accessed if you specify a comma before the number
in the bracket:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow
= 2, ncol = 2)
thismatrix[,2]
Example
thismatrix <-
matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple"
, "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix[c(1,2),]
Example
thismatrix <-
matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple"
, "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix[, c(1,2)]
Add Rows and Columns
Use the cbind() function to add additional columns in a Matrix:
Example
thismatrix <-
matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple"
, "pear", "melon", "fig"), nrow = 3, ncol = 3)
Note: The cells in the new column must be of the same length as the existing
matrix.
Example
thismatrix <-
matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple"
, "pear", "melon", "fig"), nrow = 3, ncol = 3)
Note: The cells in the new row must be of the same length as the existing
matrix.
thismatrix
Example
Check if "apple" is present in the matrix:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow
= 2, ncol = 2)
dim(thismatrix)
Matrix Length
Use the length() function to find the dimension of a Matrix:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow
= 2, ncol = 2)
length(thismatrix)
Total cells in the matrix is the number of rows multiplied by number of columns.
Example
Loop through the matrix items and print them:
Example
# Combine matrices
Matrix1 <- matrix(c("apple", "banana", "cherry", "grape"), nrow
= 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango", "pineapple", "watermelon"),
nrow = 2, ncol = 2)
# Adding it as a rows
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined
# Adding it as a columns
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined
R Arrays
Arrays
Compared to matrices, arrays can have more than two dimensions.
We can use the array() function to create an array, and the dim parameter to
specify the dimensions:
Example
# An array with one dimension with values ranging from 1 to 24
thisarray <- c(1:24)
thisarray
Example Explained
In the example above we create an array with the values 1 to 24.
Example
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[2, 3, 2]
You can also access the whole row or column from a matrix in an array, by using
the c() function:
Example
thisarray <- c(1:24)
# Access all the items from the first row from matrix one
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[c(1),,1]
# Access all the items from the first column from matrix one
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[,c(1),1]
A comma (,) before c() means that we want to access the column.
A comma (,) after c() means that we want to access the row.
ADVERTISEMENT
Example
Check if the value "2" is present in the array:
2 %in% multiarray
Example
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
dim(multiarray)
Array Length
Use the length() function to find the dimension of an array:
Example
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
length(multiarray)
Example
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
for(x in multiarray){
print(x)
}
R Data Frames
Data Frames
Data Frames are data displayed in a format as a table.
Data Frames can have different types of data inside it. While the first column
can be character, the second and third can be numeric or logical. However, each
column should have the same type of data.
Example
# Create a data frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame
summary(Data_Frame)
You will learn more about the summary() function in the statistical part of the R
tutorial.
Access Items
We can use single brackets [ ], double brackets [[ ]] or $ to access columns
from a data frame:
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame[1]
Data_Frame[["Training"]]
Data_Frame$Training
Add Rows
Use the rbind() function to add new rows in a Data Frame:
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Add Columns
Use the cbind() function to add new columns in a Data Frame:
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
dim(Data_Frame)
You can also use the ncol() function to find the number of columns
and nrow() to find the number of rows:
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
ncol(Data_Frame)
nrow(Data_Frame)
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
length(Data_Frame)
Example
Data_Frame1 <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
And use the cbind() function to combine two or more data frames in R
horizontally:
Example
Data_Frame3 <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
R Factors
Factors
Factors are used to categorize data. Examples of factors are:
Demography: Male/Female
Music: Rock, Pop, Classic, Jazz
Training: Strength, Stamina
To create a factor, use the factor() function and add a vector as argument:
Example
# Create a factor
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock
", "Jazz"))
Result:
You can see from the example above that that the factor has four levels
(categories): Classic, Jazz, Pop and Rock.
Example
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock
", "Jazz"))
levels(music_genre)
Result:
You can also set the levels, by adding the levels argument inside
the factor() function:
Example
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock
", "Jazz"), levels = c("Classic", "Jazz", "Pop", "Rock", "Other"))
levels(music_genre)
Result:
Factor Length
Use the length() function to find out how many items there are in the factor:
Example
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock
", "Jazz"))
length(music_genre)
Result:
[1] 8
Access Factors
To access the items in a factor, refer to the index number, using [] brackets:
Example
Access the third item:
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock
", "Jazz"))
music_genre[3]
Result:
[1] Classic
Levels: Classic Jazz Pop Rock
Change Item Value
To change the value of a specific item, refer to the index number:
Example
Change the value of the third item:
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock
", "Jazz"))
music_genre[3]
Result:
[1] Pop
Levels: Classic Jazz Pop Rock
Note that you cannot change the value of a specific item if it is not already
specified in the factor. The following example will produce an error:
Example
Trying to change the value of the third item ("Classic") to an item that does not
exist/not predefined ("Opera"):
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock
", "Jazz"))
music_genre[3]
Result:
Warning message:
In `[<-.factor`(`*tmp*`, 3, value = "Opera") :
invalid factor level, NA generated
However, if you have already specified it inside the levels argument, it will
work:
Example
Change the value of the third item:
music_genre <-
factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock
", "Jazz"), levels = c("Classic", "Jazz", "Pop", "Rock", "Opera"))
music_genre[3]
Result:
[1] Opera
Levels: Classic Jazz Pop Rock Opera
R - Switch Statement
A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is
checked for each case.
Syntax
The basic syntax for creating a switch statement in R is −
Flow Diagram
Example
Live Demo
x <- switch(
3,
"first",
"second",
"third",
"fourth"
)
print(x)
When the above code is compiled and executed, it produces the following result
−
[1] "third"
R - Packages
R packages are a collection of R functions, complied code and sample data. They are stored
under a directory called "library" in the R environment. By default, R installs a set of
packages during installation. More packages are added later, when they are needed for some
specific purpose. When we start the R console, only the default packages are available by
default. Other packages which are already installed have to be loaded explicitly to be used by the
R program that is going to use them.
All the packages available in R language are listed at R Packages.
Below is a list of commands to be used to check, verify and use the R packages.
Live Demo
.libPaths()
When we execute the above code, it produces the following result. It may vary depending on the
local settings of your pc.
library()
When we execute the above code, it produces the following result. It may vary depending on the
local settings of your pc.
Live Demo
search()
When we execute the above code, it produces the following result. It may vary depending on the
local settings of your pc.
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
install.packages("Package Name")
# Install the package named "XML".
install.packages("XML")
Now you can run the following command to install this package in the R environment.