Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

R Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

R Programming

R programming is an interpreted programming language widely used to analyze


statistical information and a graphical representation.

R programming is popular in the field of data science among data analysts, researchers,
statisticians, etc. You can use R to retrieve data from datasets, clean them, analyze and
visualize them, and present them in the most suitable way.

R Comments
Comments are portions of a computer program that are used to describe a
piece of code.Comments have nothing to do with code logic. They do not get
interpreted or compiled and are completely ignored during the execution of the
program.

. R Single-Line Comments
You use the # symbol to create single-line comments in R. For example,

2. R Multi-Line Comments
As already mentioned, R does not have any syntax to create multi-line
comments.

● It increases readability of the program for users other than the


developers.
● Comments in R provide metadata of the code or the overall project.
● Comments are generally used by programmers to ignore some pieces
of code during testing.
● They are used to write a simple pseudo-code of the program.
R Variables and Constants
In computer programming, a variable is a named memory location
where data is stored.

● A variable name in R can be created using letters, digits, periods, and


underscores.
● You can start a variable name with a letter or a period, but not with
digits.
● If a variable name starts with a dot, you can't follow it with digits.
● R is case sensitive. This means that age and Age are treated as different
variables.
● We have some reserved words that cannot be used as variable names.

. Boolean Variables

It stores single bit data which is either TRUE or FALSE. Here, TRUE means yes
and FALSE means no.

2. Integer Variables

It stores numeric data without any decimal values

3. Floating Point Variables

It stores numeric data with decimal values.

4. Character Variables
It stores a single character data. For example

5. String Variables

It stores data that is composed of more than one character. We use double
quotes to represent string data.

R Constants
Constants are those entities whose values aren't meant to be changed
anywhere throughout the code. In R, we can declare constants using the <-
symbol

Types of R Constants
In R, we have the following types of constants.

● The five types of R constants - numeric, integer, complex, logical,


string.

● In addition to these, there are 4 specific types of R constants - Null, NA,


Inf, NaN.

1. Integer Constants

Integer constants are the integer values we use in our code. These constants
end with the letter L.

2. Numeric Constants
In R programming, numeric constants can be integers (4), floating-point
numbers (0.55), or exponential numbers (3e-3).

3. Logical Constants

Logical constants in R are either TRUE or FALSE

4. String Constants

String constants are the string data we use in our code

5. Complex Constants

A complex constant is data that contains a real and an imaginary part


(denoted by the suffix i).

Special R Constants
R programming also provides 4 special types of constants.

● NULL - to declare an empty R object.

Inf/-Inf- represents positive and negative infinity


● LETTERS - to display a list of all uppercase letters
● letters - to display a list of all small letters
● month.abb - to print 3 letter abbreviations of all English months
● pi - to print the numerical value of the constant pi

R Data Types
A variable can store different types of values such as numbers, characters etc.
These different types of data that we can use in our code are called data
types.

1. Logical Data Type

The logical data type in R is also known as boolean data type. It can only
have two values: TRUE and FALSE

2. Numeric Data Type

In R, the numeric data type represents all real numbers with or without decimal
values

. Integer Data Type

The integer data type specifies real values without decimal points. We use the
suffix L to specify integer data.

4. Complex Data Type

The complex data type is used to specify purely imaginary values in R. We use
the suffix i to specify the imaginary part

5. Character Data Type

The character data type is used to specify character or string values in a


variable.

In programming, a string is a set of characters. For example, 'A' is a single


character and "Apple" is a string.
You can use single quotes '' or double quotes "" to represent strings. In
general, we use:

● '' for character variables

R print() Function
In R, we use the print() function to print values and variables

paste() Function in R
You can also print a string and variable together using the print() function.
For this, you have to use the paste() function inside print().

Notice the use of the paste() function inside print(). The paste() function
takes two arguments:

● string - "Welcome to"


● variable - company

R sprintf() Function
The sprintf() function of C Programming can also be used in R. It is used to
print formatted strings.

R Numbers
Numbers in R can be divided into 3 different categories:
● Numeric: It represents both whole and floating-point numbers. For
example, 123, 32.43, etc.
● Integer: It represents only whole numbers and is denoted by L. For
example, 23L, 39L, etc.
● Complex: It represents complex numbers with imaginary parts. The
imaginary parts are denoted by i. For example, 2 + 3i, 5i, etc.

R if Statement
x <- 3

# check if x is greater than 0

if (x > 0) {

print("The number is positive")

print("Outside if statement")

R if...else Statement
age <- 15

# check if age is greater than 18


if (age > 18) {
print("You are eligible to vote.")
} else {
print("You cannot vote.")
}
R if...else if...else
x <- 0

# check if x is positive or negative or zero


if (x > 0) {
print("x is a positive number")
} else if (x < 0) {
print("x is a negative number")
} else {
print("x is zero")
}

R while Loop
while loops are used when you don't know the exact number of times a
block of code is to be repeated

number = 1

# while loop to print numbers from 1 to 5


while(number <= 10) {
print(number)

# increment number by 1
number = number + 1

# break if number is 6
if (number == 6) {
break
}
}

R for Loop
A for loop is used to iterate over a list, vector or any other object of elements.
The syntax of for loop is:

# vector of numbers
num = c(2, 3, 12, 14, 5, 19, 23, 64)

# variable to store the count of even numbers


count = 0

# for loop to count even numbers


for (i in num) {

# check if i is even
if (i %% 2 == 0) {
count = count + 1
}
}

print(count)

R break and next


We use the R break and next statements to alter the flow of a program.
These are also known as jump statements in programming:

● break - terminate a looping statement


● next - skips an iteration of the loop

R Functions
Introduction to R Functions
A function is just a block of code that you can call and run from any part of
your program. They are used to break our code in simple parts and avoid
repeatable codes.

You can pass data into functions with the help of parameters and return some
other data as a result. You can use the function() reserve keyword to create a
function in R. The syntax is:

func_name <- function (parameters) {


statement
}

# define a function to compute power


power <- function(a, b) {
print(paste("a raised to the power b is: ", a^b))
}
# call the power function with arguments
power(2, 3)

Named Arguments
# define a function to compute power

power <- function(a, b) {

print(paste("a raised to the power b is: ", a^b))

# call the power function with arguments

power(b=3, a=2)

Return Values
You can use the return() keyword to return values from a function. For
example,
# define a function to compute power

power <- function(a, b) {

return (a^b)

# call the power function with arguments

print(paste("a raised to the power b is: ", power(2, 3)))

Write a Function Inside Another Function

Let's create a nested function by writing a function inside another function.

# define a function to compute power

power <- function(a) {

exponent <- function(b) {

return (a^b)

return (exponent)

# call nested function

result <- power(2)

print(result(3))

R Strings
A string is a sequence of characters. For example, "Programming" is a string
that includes characters: P, r, o, g, r, a, m, m, i, n, g.

In R, we represent strings using quotation marks (double quotes, " " or single
quotes, ' ').
message2 <- "Welcome to Programiz"

print(message2)

1. Find Length of R String


We use the nchar() method to find the length of a string

2. Join Strings Together


In R, we can use the paste() function to join two or more strings together

Compare Two Strings in R Programming


We use the == operator to compare two strings. If two strings are equal, the

o4. Change Case of R String

In R, we can change the case of a string using

● toupper() - convert string to uppercase


● tolower() - convert string to lowercase

perator returns TRUE. Otherwise, it returns FALSE

4. Change Case of R String


In R, we can change the case of a string using

● toupper() - convert string to uppercase


● tolower() - convert string to lowercase
Escape Sequences in R String
The escape sequence is used to escape some of the characters present
inside a string.

R Vectors
A vector is the basic data structure in R that stores data of similar types

Create a Vector in R
In R, we use the c() function to create a vector

employees <- c("Sabby", "Cathy", "Lucy")

print(employees)

Access Vector Elements in R


● # a vector of string type
● languages <- c("Swift", "Java", "R")

● # access first element of languages
● print(languages[1]) # "Swift"

● # access third element of languages
● print(languages[3]). # "R"

Modify Vector Element


dailyActivities <- c("Eat","Repeat")

cat("Initial Vector:", dailyActivities)

# change element at index 2

dailyActivities[2] <- "Sleep"

cat("\nUpdated Vector:", dailyActivities)

Numeric Vector in R
● # a vector with number sequence from 1 to 5
● numbers <- c(1, 2, 3, 4, 5)

● print(numbers)

Create a Sequence of Number in R


# a vector with number sequence from 1 to 5

numbers <- 1:5

Loop Over a R Vector


numbers <- c(1, 2, 3, 4, 5)

# iterate through each elements of numbers

for (number in numbers) {

print(number)

Length of Vector in R
We can use the length() function to find the number of elements present
inside the vector.

languages <- c("R", "Swift", "Python", "Java")

# find total elements in languages using length()

cat("Total Elements:", length(languages))


R Matrix
A matrix is a two-dimensional data structure where data are arranged into
rows and columns.

Create a Matrix in R
In R, we use the matrix() function to create a matrix.

The syntax of the matrix() function is

● vector - the data items of same type


● nrow - number of rows
● byrow (optional) - if TRUE, the matrix is filled row-wise. By default, the
matrix is filled column-wise.
● ncol - number of columns

# create a 2 by 3 matrix

matrix1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)

print(matrix1)

Access Matrix Elements in R


We use the vector index operator [ ] to access specific elements of a matrix
in R.

The syntax to access a matrix element is


matrix[n1, n2]

matrix1 <- matrix(c("Sabby", "Cathy", "Larry", "Harry"), nrow = 2, ncol =


2)

print(matrix1)

# access element at 1st row, 2nd column

cat("\nDesired Element:", matrix1[1, 2])

In R, we can also access the entire row or column based on the value passed
inside [].

● [n, ] - returns the entire element of the nth row.


● [ ,n] - returns the entire element of the nth column.

matrix1 <- matrix(c("Sabby", "Cathy", "Larry", "Harry"), nrow = 2, ncol =


2)

print(matrix1)

# access entire element at 1st row

cat("\n1st Row:", matrix1[1, ])

# access entire element at 2nd column

cat("\n2nd Column:", matrix1[, 2])

Modify Matrix Element in R


We use the vector index operator [] to modify the specified element. For
example,

matrix1[1,2] = 140

Combine Two Matrices in R


In R, we use the cbind() and the rbind() function to combine two matrices
together.

● cbind() - combines two matrices by columns


● rbind() - combines two matrices by rows

# create two 2 by 2 matrices

even_numbers <- matrix(c(2, 4, 6, 8), nrow = 2, ncol = 2)

odd_numbers <- matrix(c(1, 3, 5, 7), nrow = 2, ncol = 2)

# combine two matrices by column

total1 <- cbind(even_numbers, odd_numbers)

print(total1)

# combine two matrices by row

total2 <- rbind(even_numbers, odd_numbers)

print(total2)

R List
A List is a collection of similar or different types of data.

In R, we use the list() function to create a list

Access List Elements in R


list1 <- list(24, "Sabby", 5.4, "Nepal")

# access 1st item

print(list1[1]) # 24

# access 4th item


print(list1[4]) # Nepal

Modify a List Element in R


list1 <- list(24, "Sabby", 5.4, "Nepal")

# change element at index 2

list1[2] <- "Cathy"

# print updated list

print(list1)

Add Items to R List


We use the append() function to add an item at the end of the list. For example

list1 <- list(24, "Sabby", 5.4, "Nepal")

# using append() function

append(list1, 3.14)

Remove Items From a List in R


R allows us to remove items for a list. We first access elements using a list
index and add negative sign - to indicate we want to delete the item. For
example,

● [-1] - removes 1st item


● [-2] - removes 2nd item and so on.

Length of R List
In R, we can use the length() function to find the number of elements present
inside the list. For example,
R Array
An Array is a data structure which can store data of the same type in more
than two dimensions.

The only difference between vectors, matrices, and arrays are

● Vectors are uni-dimensional arrays


● Matrices are two-dimensional arrays
● Arrays can have more than two dimensions

Create an Array in R
In R, we use the array() function to create an array.

The syntax of the array() function is

array(vector, dim = c(nrow, ncol, nmat))

Here,

● vector - the data items of same type


● nrow - number of rows
● ncol - number of columns
● nmat - the number of matrices of nrow * ncol dimension

# create two 2 by 3 matrix

array1 <- array(c(1:12), dim = c(2,3,2)


print(array1)

Access Array Elements


array[n1, n2, mat_level]

# create two 2 by 3 matrix

array1 <- array(c(1:12), dim = c(2,3,2))

print(array1)

# access element at 1st row, 3rd column of 2nd matrix

cat("\nDesired Element:", array1[1, 3, 2])

Access Entire Row or Column

In R, we can also access the entire row or column based on the value passed
inside [].

● [c(n), ,mat_level] - returns the entire element of the nth row.


● [ ,c(n), mat_level] - returns the entire element of the nth column.

For example,

# create a two 2 by 3 matrix

array1 <- array(c(1:12), dim = c(2,3,2))

print(array1)

# access entire elements at 2nd column of 1st matrix

cat("\n2nd Column Elements of 1st matrix:", array1[,c(2),1])


# access entire elements at 1st row of 2nd matrix

cat("\n1st Row Elements of 2nd Matrix:", array1[c(1), ,2])

R Data Frame
A data frame is a two-dimensional data structure which can store data in
tabular format.

Data frames have rows and columns and each column can be a different
vector. And different vectors can be of different data types.

# Create a data frame

dataframe1 <- data.frame (

Name = c("Juan", "Alcaraz", "Simantha"),

Age = c(22, 15, 19),

Vote = c(TRUE, FALSE, TRUE)

print(dataframe1)

Access Data Frame Columns


There are different ways to extract columns from a data frame. We can use [
], [[ ]], or $ to access specific column of a data frame in R

Create a data frame

dataframe1 <- data.frame (

Name = c("Juan", "Alcaraz", "Simantha"),


Age = c(22, 15, 19),

Vote = c(TRUE, FALSE, TRUE)

# pass index number inside [ ]

print(dataframe1[1])

# pass column name inside [[ ]]

print(dataframe1[["Name"]])

# use $ operator and column name

print(dataframe1$Name)

Combine Data Frames


In R, we use the rbind() and the cbind() function to combine two data frames
together.

● rbind() - combines two data frames vertically


● cbind() - combines two data frames horizontally

Combine Vertically Using rbind()

If we want to combine two data frames vertically, the column name of the two
data frames must be the same. For example,

# create a data frame

dataframe1 <- data.frame (

Name = c("Juan", "Alcaraz"),

Age = c(22, 15)


)

# create another data frame

dataframe2 <- data.frame (

Name = c("Yiruma", "Bach"),

Age = c(46, 89)

# combine two data frames vertically

updated <- rbind(dataframe1, dataframe2)

print(updated)

cbind()
# create a data frame

dataframe1 <- data.frame (

Name = c("Juan", "Alcaraz"),

Age = c(22, 15)

# create another data frame

dataframe2 <- data.frame (

Hobby = c("Tennis", "Piano")

# combine two data frames horizontally

updated <- cbind(dataframe1, dataframe2)print(updated)


R Factors
A Factor is a data structure that is used to work with categorizable datas.

Suppose a data field such as marital status may contain only values from
single, married, separated, divorced, or widowed.

In such a case, we know the possible values beforehand and these


predefined, distinct values are called levels of a factor.

Create a Factor in R
In R, we use the factor() function to create a factor. Once a factor is created,
it can only contain predefined set values called levels.

The syntax for creating a factor is

factor(vector)

# create a factor

students_gender <- factor(c("male", "female", "male", "transgender",


"female"))

# print the marital_status factor

print(students_gender)

Access Factors Elements


Accessing vector elements is similar to that of vectors. We use the index
number. For example,

# create a factor
students_gender <- factor(c("male", "female", "male", "transgender",
"female"))

# access 1st element of students_gender

print(students_gender[1])

# access 4th element of students_gender

print(students_gender[4])

R Bar Plot
Bar Plots is one of the most efficient ways of representing datas. It can be
used to summarize large data in visual form.

Bar graphs have the ability to represent data that shows changes over time,
which helps us to visualize trends.

Create Bar Plot in R


In R, we use the barplot() function to create bar plots. For example,

temperatures <- c(22, 27, 26, 24, 23, 26, 28)

result <- barplot(temperatures,

main = "Maximum Temperatures in a Week",

xlab = "Degree Celsius",

ylab = "Day",

names.arg = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),


col = "blue"

print(result)

Output

R Pie Chart
A pie chart is a circular statistical graphic, which is divided into slices to
illustrate numerical proportion.

Pie charts represents data visually as a fractional part of a whole, which can
be an effective communication tool.

expenditure <- c(600, 300, 150, 100, 200)


result <- pie(expenditure,

main = "Monthly Expenditure Breakdown",

labels = c("Housing", "Food", "Cloths", "Entertainment", "Other"),

col = c("red", "orange", "yellow", "blue", "green")

print(result)

Output

You might also like