RStudio

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 60

R PROGRAMMING

Data Types
• A data type is a classification of data which tells the compiler or
interpreter how the programmer intends to use the data.
• 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:
• y_var <- 30 # my_var is type of numeric
my_var <- "Sally" # my_var is now of type character (a string)

OUTPUT:
• "Sally"
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 (string) - ("k", "R is exciting", "FALSE", "11.5")
• logical ( boolean) - (TRUE or FALSE)
We can use the class() function to check the data type of a
variable:
# numeric
x <- 10.5
class(x)

# integer
x <- 1000L
class(x)
"numeric"

# complex "integer"
x <- 9i + 3
class(x) "complex"
# character/string "character"
x <- "R is exciting"
class(x)
"logical"
# logical/boolean
x <- TRUE
class(x)
Integer:
• 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:

x <- 1000L
y <- 55L
# Print values of x and y
1000
x
55
y “integer"
# Print the class name of x and y "integer"
class(x)
class(y)
Type Conversion
• You can convert from one type to another with the following
functions:
x <- 1L # integer
as.numeric() y <- 2 # numeric
# convert from integer to numeric: "numeric"
as.integer() a <- as.numeric(x)
"integer"
as.complex() # 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)
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:
• min() and max() functions can be used to find the lowest or highest number
in a set.
• The function sqrt() returns square root of a number

max(5, 10, 15) 15


5
min(5, 10, 15) 4

sqrt(16)
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‘
• String Length: to find the number of characters in a string, use the nchar()
function:

str <- "Hello World!" 12

nchar(str)
Operators
• 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:
R Assignment Operators

Assignment operators are used to assign values to variables:


my_var <- 3

my_var <<- 3

3 -> my_var

3 ->> my_var

my_var # print my_var value 3

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
R Vectors
• A vector is simply a list of items that are of the same type.
• 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:
# Vector of strings
fruits <- c("banana", "apple", "orange")

# Print fruits
fruits
"banana" "apple" "orange"

# Vector of numerical values


numbers <- c(1, 2, 3)
1 2 3
# Print numbers
numbers
To create a vector with numerical values in a sequence, use the : operator

# Vector with numerical values in a sequence


numbers <- 1:10
1 2 3 4 5 6 7 8 9 10
numbers

# Vector with numerical decimals in a sequence 1.5 2.5 3.5 4.5 5.5 6.5
numbers1 <- 1.5:6.5 1.5 2.5 3.5 4.5 5.5
numbers1

# Vector with numerical decimals in a sequence where the last


element is not used
numbers2 <- 1.5:6.3
numbers2
1 2 3 4 5 6 7 8 9 10
In the example below, we create a vector of logical values:

# Vector of logical values


log_values <- c(TRUE, FALSE, TRUE, FALSE) TRUE FALSE TRUE FALSE
log_values
Vector Length:
To find out how many items a vector has, use the length() function

fruits <- c("banana", "apple", "orange")

length(fruits)

3
Sort a Vector
To sort items in a vector alphabetically
or numerically, use
the sort() function:
fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)

sort(fruits) # Sort a string


sort(numbers) # Sort numbers

[1] "apple" "banana" "lemon" "mango" "orange"


[1] 2 3 5 7 13 20
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:
fruits <- c("banana", "apple", "orange")

# Access the first item (banana)


fruits[1]
"banana"
You can also access multiple elements by referring
to different index positions with the c() function:

Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")

# Access the first and third item (banana and orange)


fruits[c(1, 3)]
Output:
[1] "banana" "orange"
You can also use negative index numbers to access all items except the
ones specified:
Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")

# Access all items except for the first item


fruits[c(-1)]
Output :
[1] "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")

# Change "banana" to "pear"


fruits[1] <- "pear"

# Print fruits
fruits
Output :
"pear" "apple" "orange" "mango" "lemon"
R 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.
To create a list, use the list() function:
Example :

list<-list("hello",c(1,2,3,4),45.6,"hi")
print(list)
• Example 2:
• thislist <- list("apple", "banana", "cherry")

thislist[1]
Naming elements in a list :
• list<-list("hello",c(1,2,3,"4"),45.6,"hi")
• names(list)<-c("string","vector","double","string")
• print(list)
Using unlist() to perform arithmetic:
list1=list(5:9)
list2=list(20:24)
v1=unlist(list1)
v2=unlist(list2)
print(v1)
print(v2)
result=v1+v2
print(result)
Merging of lists [[1]]
[1] 1
• Merging of list can be done by placing the lists in
the c() function or the list function. [[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] "Sun"

[[5]]
[1] "Mon"

[[6]]
[1] "Tue"
R – Matrices

• Matrix is a rectangular arrangement of numbers in rows and columns.


• Rows are the ones that run horizontally and columns are the ones that
run vertically.
• In R programming, matrices are 2-dimensional, homogeneous
data structures. These are some examples of matrices:
• A matrix can be created with the matrix() function.
• Specify the nrow and ncol parameters to get the amount of
rows and columns:
Syntax :matrix(data, nrow, ncol, byrow, dimnames)

Example
• # Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)

# Print the matrix


thismatrix
You can also create a matrix with strings:
Example:
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2,
ncol = 2)

thismatrix
Access Matrix Items

• thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow


= 2, ncol = 2)

thismatrix[1, 2]
byrow : represent a logical clue . When set to
FALSE , then the elements in input vector are
organized by column
• thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow =
2, ncol = 2,byrow=FALSE)

• thismatrix
byrow : represent a logical clue . When set to
TRUE , then the elements in input vector are
organized by row
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2,
ncol = 2,byrow=TRUE)

thismatrix
dimname : is the name assigned to rows and
columns
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

rownames=c('a','b')

colnames=c(1,2)

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2,


dimnames=list(rownames,colnames))

thismatrix
How to access elements in a matrix?
rownames=c("row1","row2","row3")

colnames=c("col1","col2","col3","col4")

p=matrix(c(2:13),nrow=3,byrow=TRUE, dimnames=list(rownames,
colnames))

print(p[1,3])

print(p[3,2])
Matrix where no data source is provided

Y<-matrix(nrow=2, ncol=2)

Y
Arithmetic operations on matrices
• Arithmetic operations such as addition, subtraction, multiplication &
Division can be performed on Matrices
• For performing arithmetic operations, the two matrices must be of
the same dimensions.
• The result generated is also a matrix.
MATRIX 1
[,1] [,2] [,3]
[1,] 0 -2 8
[2,] 5 3 7
MATRIX 2
[,1] [,2] [,3]
[1,] 9 0 5
[2,] 2 1 3
Result of Addition
[,1] [,2] [,3]
[1,] 9 -2 13
[2,] 7 4 10
Result of Subraction
[,1] [,2] [,3]
[1,] -9 -2 3
[2,] 3 2 4
Result of Multiplication
[,1] [,2] [,3]
[1,] 0 0 40
[2,] 10 3 21
Result of DIvision
[,1] [,2] [,3]
[1,] 0.0 -Inf 1.600000
[2,] 2.5 3 2.333333
# create a vector of elements
vector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

# create a matrix with 4* 4 by passing this vector1


matrix1 <- matrix(vector1, nrow = 4, ncol = 4)

# display matrix
print(matrix1)

# create a vector of elements


vector2=c(1,2,3,2,4,5,6,3,4,1,2,7,8,9,4,5)

# create a matrix with 4* 4 by passing vector2


matrix2 <- matrix(vector2, nrow = 4, ncol = 4)

# display matrix
print(matrix2)

# add matrices
print(matrix1+matrix2)
Creating Matrix taking vector of numbers as input
# Elements arranged sequentially by row

M <- matrix(c(2:13), nrow=3, byrow=TRUE)

print(M)

# Elements arranged sequentially by Column

N <- matrix(c(2:13), nrow=3, byrow=FALSE)

print(N)

#Define row and column names

rownames=c("row1", "row2", "row 3")

colnames=c("col1", "col2", "col 3", "col4")P<- matrix(c(2:13), nrow=3, byrow=TRUE,


dimnames=list(rownames, colnames))

print(P)
Arithmetic operations on Matrices :
Matrix1 <- matrix(c(1,3,-4,5), nrow = 2, ncol = 2)
print(Matrix1)
Matrix2 <- matrix(c(3,-5,7,6), nrow = 2, ncol = 2)
print(Matrix2)
result=Matrix1+Matrix2
print(result)
result=Matrix1-Matrix2
print(result)
Arrays
• Array are R objects which can store data in more than two
dimensions.
• Function array() function takes vectors as input & uses dim parameter
values to create array.
,,1

[,1] [,2] [,3]


[1,] 6 9 16
[2,] 4 12 14
[3,] 1 15 11

,,2

[,1] [,2] [,3]


[1,] 6 9 16
[2,] 4 12 14
[3,] 1 15 11
# An array with one dimension with values ranging from 1 to 24
thisarray <- c(1:24)
thisarray

# An array with more than one dimension


multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray
Access Array Items in first matrix :

thisarray <- c(1:24)


multiarray <- array(thisarray, dim = c(4, 3, 2))
print(multiarray)
multiarray[2, 3, 1]
Access Array Items in second matrix :

thisarray <- c(1:24)


multiarray <- array(thisarray, dim = c(4, 3, 2))
print(multiarray)

multiarray[2, 3, 2]
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
print(multiarray)
multiarray[,, 2]
FACTORS IN R PROGRAMMING :
• Factors are implemented to categorize the data or represent categorical data
and store it on multiple levels.
• Can store both integers and strings
• R factor accepts only a restricted number of distinct values.
• For example, a data field such as gender may contain values only from female,
male, or other.
• These distinct values are known as levels.
• After a factor is created it only consists of levels that are by default sorted
alphabetically.
• Helpful in the data analysis for statistical modelling
• Using a function called as factor()
• Input as vector values.
Creating a Factor in R Programming Language

• The command used to create or modify a factor in R language is


– factor() with a vector as input.
• The two steps to creating an R factor :
• Creating a vector
• Converting the vector created into a factor using function factor()
is.factor() function in R Language is used to check if the object passed
to the function is a Factor or not. It returns a boolean value as output.

d=c("right", "left", "right", "left", "left", "left", "right", "top")


print(d)
print(is.factor(d))

# APPLY THE FACTOR FUNCTION


fd=factor(d)
print(fd)
print(is.factor(fd))
# Creating a vector
x<-c("female", "male", "male", "female")

# Converting the vector x into a factor


named gender
gender<-factor(x)

# Using is.factor() Function


is.factor(gender)
[1] "right" "left" "right" "left" "left" "left" "right" "top"
[1] FALSE
[1] right left right left left left right top
Levels: left right top
[1] TRUE
Data Frame in R :
#create the data frame
person.data=data.frame(emp_id=c(1:5),
emp_name=c("ravi","ram","alex","dan","ryan"),
age=c(23,22,11,12,13))
print(person.data)
emp_id emp_name age
1 ravi 23
2 ram 22
3 alex 11
4 dan 12
5 ryan 13
Structure of the data frame:
#create the data frame
person.data=data.frame(emp_id=c(1:5),
emp_name=c("ravi","ram","alex","dan","ryan"),
age=c(23,22,11,12,13))
str(person.data)
Summary() Function

• To get the statistical summary and nature of data in a data frame


summary() function can be used.
How to extract data from data frame ?
#extract data from data frame
person.data=data.frame(emp_id=c(1:4),
emp_name=c("ravi","ram","alex","dan"),
age=c(23,22,11,12))
result=data.frame(person.data$emp_id,person.data$age)
print(result)
R – Line Graphs
The plot() function in R is used to create the line graph.
Syntax: plot(v, type, col, xlab, ylab)
Parameters:
v: This parameter is a contains only the numeric values
type: This parameter has the following value:
“p” : This value is used to draw only the points.
“l” : This value is used to draw only the lines.
“o”: This value is used to draw both points and lines
xlab: This parameter is the label for x axis in the chart.
ylab: This parameter is the label for y axis in the chart.
main: This parameter main is the title of the chart.
col: This parameter is used to give colors to both the points and lines .

# Create the data for the chart.


v <- c(17, 25, 38, 13, 41)

# Plot the bar chart.


plot(v, type = "o")
Adding Title, Color and Labels in Line Graphs in R

# Create the data for the chart.


v <- c(17, 25, 38, 13, 41)

# Plot the bar chart.


plot(v, type = "o", col = "green",
xlab = "Month", ylab = "Article Written",
main = "Article Written chart")
Multiple Lines in a Line Graph in R Programming Language

# Create the data for the chart.


v <- c(17, 25, 38, 13, 41)
t <- c(22, 19, 36, 19, 23)
m <- c(25, 14, 16, 34, 29)

# Plot the bar chart.


plot(v, type = "o", col = "red",
xlab = "Month", ylab = "Article Written ",
main = "Article Written chart")

lines(t, type = "o", col = "blue")


lines(m, type = "o", col = "green")
R – Histograms
We can create histograms in R Programming Language using the hist() function.

• Syntax: hist(v, main, xlab, xlim, ylim, breaks, col, border)


• Parameters:
• v: This parameter contains numerical values used in histogram.
• main: This parameter main is the title of the chart.
• col: This parameter is used to set color of the bars.
• xlab: This parameter is the label for horizontal axis.
• border: This parameter is used to set border color of each bar.
• xlim: This parameter is used for plotting values of x-axis.
• ylim: This parameter is used for plotting values of y-axis.
• breaks: This parameter is used as width of each bar.
Creating a simple Histogram in R

# Create data for the graph.


v <- c(19, 23, 11, 5, 16, 21, 32,
14, 19, 27, 39)

# Create the histogram.


hist(v, xlab = "No.of Articles ",
col = "green", border =
"black")
Range of X and Y values

# Create data for the graph.


v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39)

# Create the histogram.


hist(v, xlab = "No.of Articles", col = "green",
border = "black", xlim = c(0, 50),
ylim = c(0, 5), breaks = 5)

You might also like