R PROGRAMMING
1. Write a R program for different types of data structures in R.
Adata structure is a particular way of organizing data in a computer so that it
can beused
effectively. It used to reduce the space and time complexities of different tasks, Data
structures in R programming are tools for holding multiple values. R gives six data
types which are most frequently utilized in data analysis. Each data structure has its
own use cases and properties, and we can perform various operations on them to.
manipulate and analyse data
PROGRAM:
VECTORS
A vector is an ordered collection of basic data types of a given length. All the
elements of
a vector must be of the identical data type e.g. homogeneous data structures.
Vectors are
one-dimensional data structures
# Vector
x <¢ (‘Jan’,'Feb"’March’,"Apr’/May","June" July")
y~e (1,234)
print(x)
print(y)
OUTPUT
[1] Jan" "Feb" ‘March’ "Apr" "May" "June" "July"
(111234
LISTS
A list is a generic object consisting of an ordered collection of objects. Lists are
heterogenzous data structures, These are also onedimensional date structures. A
list can
be a list of vectors, list of matrices, a list of characters and a list of functions and so
on.
# List
listI< list(’Sam’, "Green’, o(3,4,2), TRUE, 11.78, FALSE)
print(list1)
OUTPUT
(0)
fi]'sam"
(2
[1] "Green"{3}
[1]342
(4
[1] TRUE
(5)
[1] 11.78
{ell
[1] FALSE
DATAFRAMES
Data frames are generic data objects of R which are used to store the tabular data.
Data frames are the foremost popular data objects in R programming because we
are comfortable in seeing the data within the tabuler form. They are two-
dimensional, heterogeneous data structures.
empid <- o(1:4)
empname <- o('Sam’"Ram"Raja"/John")
empdept <- ¢("Sales"”Marketing’/HR"/R & D")
emp.data <- data.frame(empidempnameempdept)
print(emp.data)
OUTPUT
empid empname empdept
1 Sam Sales
2 Ram Marketing
3 Raja HR
4 John R&D
DATAFRAME WITH TIME-SERIES
# DataFrame with time-series
date <- as Date(c’2023-01-01"'2023-01-02",'2023-01-03"))
value < (100, 110, 105)
df_time_series <- data.frame(Date = date, Value = value)
print(df_time_series)
OUTPUT
Date Value
1 2023-01-01 100
2 2023-01-02 110
3 2023-01-03 105MATRICES
A matrix is a rectangular arrangement of numbers in rows and columns. Ina matrix,
as we know rows are the ones that run horizontally and columns are the ones that
run vertically. Matrices are two-dimensional, homogeneous data structures.
matrix new < matrix(cbinds- (1 4,6) ,rbind <- ¢(3,7,8)))
print(matrix_new)
M1 <- matrix(c(1:9), nrow = 3, ncol =3, byrow= TRUE)
print(M1)
OUTPUT
(1) 62) £3)
fh] 123
[21 4 5 6
[3] 7 8 9
ARRAYS
Arrays are the R data objects which store the data in more than two dimensions.
Arrays are n-
dimensional data structures. For example, if we create an array of dimensions (2, 3,
3) then it
creates 3 rectangular matrices each with 2 rows and 3 columns. They are
homogeneous data
structures.
A= array(c(1, 2, 3, 4,5, 6, 7,8),dim = ¢(2, 2, 2))
print(A)
OUTPUT
wl
(1) £2]
f}13
[2]24
12
(1) £2]
f1]57
[2]68
FACTORS
Factors are the data objects which are used to categorize the data and store it as
levels. They are useful for storing categorical data. They can store both strings and
integers. They are useful to categorize unique values in columns like "TRUE" or
-“FALSE”, or “MALE” or "FEMALE", etc.. They are useful in data analysis for statistical
modeling.
data <-factor(c(’Male""Female”"Male’’Child'’Child'’Male''Female";Female"))
print(data)
fac = factor(c("Male’, "Female’, “Male”, "Male’, “Female” “Male”, "Female”)
print(Fac)
OUTPUT:
[1] Male Female Male Male Female Male Female
Levels: Female Male
2Mrite a R program that include variables, constants, data types.
#data type
x=456
e = as.integer(3)
str
print(class(x))
print(class(e))
print(class(y))
print(class(z))
print(class(str1))
OUTPUT
[1] 'numeric"
[1] ‘integer’
[1] integer"
[1] complex"
[1] ‘character’
#constants
#predefined contants
print(LETTERS)
print(pi)
print(month.name)
print(month.abb)
OUTPUT
[AUB On DEN EMG” HET UKE MNT 0" PQ! REIS" TE UT"[23] "'w''x""y"'2"
[1]3.141593
[1] "January" "February" "March" "April" "May" "June" — “July”
[8]"August" "September" ‘October "November" ‘December’
[1] Jan" "Feb’ "Mar" "Apr" "May" "Jun" "Jul" "Aug’ "Sep" "Oct" "Nov" "Dec"
#numeric constant
Ac-5L
print(typeof(A))
Be- 5i
print(typeof(B))
#character constants
x<5!
print(typeof(x))
print(typeof(‘program)))
OUTPUT
[1] ‘integer"
[1] "complex"
[1] "character"
[i] "character"
# variables
m< 45.6
print(m)
y <'R progranming’
print(y)
OUTPUT
[1] 45.6
[11'R progranming"
3. Write a R program that include different operators, control structures,
default values for arguments, retuming complex objects.
#operators
#R program to illustrate
# the use of Arithmetic operatorsvec! <:0(0, 2)
vec? <:o(2, 3)
# Performing operations on Operands
cat ("Addition of vectors :", vecl + vec2, "\n")
cat (‘Subtraction of vectors", vec - vec2, "\n")
cat ("Multiplication of vectors :", veel * vec2,"\n')
cat (‘Division of vectors", vec / vec2, "\n")
cat ("Modulo of vectors :", vec1 %% vec2, "\n')
cat ("Power operator:", vec * vec2)
# R program to illustrate
# the use of Logical operators
vect <-¢(0,2)
vec? < c(TRUE,FALSE)
# Performing operations on Operands
cat ("Element wise AND :", vec & vec2, "\n")
cat ("Element wise OR :”, vec | vec2, "\n")
cat ("Logical AND :", vecl && vec2, "\n")
cat ("Logical OR", vec || vec2, "\n")
cat ("Negation :", 1vec1)
# R program to illustrate
# the use of Relational operators
vecl < (0, 2)
vec2 < (2, 3)
# Performing operations on Operands
cat ("Vector1 less than Vector2:", vecl < vec?, "\n")
cat ("Vector1 less than equal to Vector2 :', vecl <= vec?, "\n')
cat ("Vector1 greater than Vector2 :", vecl > vec2, "\n")
cat ("Vector greater than equel to Vector2 :", vecT >= vec2, "\n")
cat ("Vector not equal to Vector? “", vec != vec2, "\n")
#R program to illustrate
# the use of Assignment operators
veot = 0(2:5)
(2:5) >> vec2
vec <<- o(2:5)
veod = (2:5)
(2:5) > vec5
# Performing operations on Operands
cat (‘vector 1:", vect, "\n')
cat("vector 2“, vec2, "\n’)
cat ("vector 3:", veo3, "\n')cat("vector 4", vec, "\n")
cat("vector 5", vec5)
OUTPUT
Addition of vectors :2 5
Subtraction of vectors :-2-1
Multiplication of vectors : 06
Division of vectors : 0 0.6666657
Modulo of vectors : 0 2
Power operator :0 8
Element wise AND : FALSE FALSE
Element wise OR : TRUE TRUE
Negation : TRUE FALSE
Vector! less than Vector2 : TRUE TRUE
Vector! less than equal to Vector2 : TRUE TRUE
Vector! greater than Vector2 : FALSE FALSE
Vector greater than equal to Vector2 : FALSE FALSE
Vector! not equal to Vector2 : TRUE TRUE
vector 1:2345
vector2:2345
vector 3:2345
vector 4:2345
vector 5:2345
# Control Structures
x 10){
print(paste(s, "is greater than 10°))
Jelse(
print(paste(,, "is less than 10"))
}
OUTPUT
[1] ‘Sis less than 10°
x < letters[4:10]
# for loop
for(i in x){
print(i)
}OUTPUT
i'd"
hl’
nner
f]'s"
(i) "h"
nye
ay
# while loop
x=1
# Print] to 5
while(x <= 5){
print(x)
x=x41
}
OUTPUT
fa
hz
{3
t4
fs
x=l
# Print1 to S
repeat{
print(x)
X=x41
if(x> 5){
break
}
}
(jt
hz
hs
t4
fs
# Checks value is either positive, negative or zero
func <-function(x){
if (> Of
return('Positive")~~ Jelse if(x < 0){
return(‘Negative’)
Jelset
return(‘Zero’)
}
}
fune(1)
func(0)
func(-1)
OUTPUT
[1] Positive"
[1] Zero"
[1] 'Negative"
# R program to check if
# object is of complex type
# Calling is.complex() function
is.complex(1 + Oi)
is.complex(1.5 + 2i)
is.complex(-1.5 + 2i)
OUTPUT
[1] TRUE
(1] TRUE
[1] TRUE
4, Write a R program for calculating cumulative sums, and products minima
maxima and calculus
#R program to illustrate
# the use of cumsum() Function
# Creating Vectors
x1 < (2, 4, 5,7)
x2 <¢(2.4, 5.6, 3.4)
# Calling cumsum( Function
cumsum(x1)
cumsum(x2)
Output:~~ [i] 2 61118
[1] 2.4 8.0114
# R program to illustrate
# the use of cumprod() Function
data < c(2, 4, 5,7)
data2 < c(2.4,5.6, 3.4)
# Calling cumprod() Function
cumprod(data1)
cumprod(data2)
Output
[1] 2 8 40280
[1] 2.400 13.440 45.696
# create a vector
data = 0(23, 4, 56, 21, 34, 56, 73)
# get the minimum value
print(min(data))
# get the maximum value
print(max(data))
OUTPUT
tla
fi} 73
#Basic Calculus
install packages("mosaic’)
library(mosaic)
f<- makeFun(m*xtb~x.m=3.5,b=10)
f(x=2)
output
(17
install.packages('mosaic’)
library(mosaic)~ f<- makeFun(m*x+b~x,m=3.5,b=10)
f(x=2)
OUTPUT
[1]3
5. Write a R program for any visual representation of an object with creating
graphs using graphic functions:
Plot(),Hist(),Linechart(),Pie(),Boxplot(),Scatterplots().
Program:
#Displaying plot title with color
plot(c(1,3,5,7,9,11),c(2,7,5,10,8,10) type="o\lty=3, col="red w=
graph’,col.main="blue’)
Lmain="This is a
# Create data for the Histogram graph
v