Assignment 3 - Merged
Assignment 3 - Merged
R
Mehul Rajput
2024-09-20
p_vector<-c("apple", "banana", "grapes", "cheery", "watermelon")
p_vector
sorting_vector<-sort(p_vector)
sorting_vector
(p_vector[1])
## [1] "apple"
(p_vector[5])
## [1] "watermelon"
(p_vector[c(1,3,5)])
p_vector[3]<-"mango"
p_vector
2024-09-20
#List of Different Fruits
different_fruits<-list("mango","apple","orange")
different_fruits
## [[1]]
## [1] "mango"
##
## [[2]]
## [1] "apple"
##
## [[3]]
## [1] "orange"
first_fruit<-different_fruits[[1]]
print(first_fruit)
## [1] "mango"
#Changing Values
different_fruits[[2]]<-"watermelon"
print(different_fruits)
## [[1]]
## [1] "mango"
##
## [[2]]
## [1] "watermelon"
##
## [[3]]
## [1] "orange"
lengths_of_fruits<-length(different_fruits)
print(lengths_of_fruits)
## [1] 3
#Add item
different_fruits<-append(different_fruits,"Grapes")
print(different_fruits)
## [[1]]
## [1] "mango"
##
## [[2]]
## [1] "watermelon"
##
## [[3]]
## [1] "orange"
##
## [[4]]
## [1] "Grapes"
#Removing Item
different_fruits[3]<-NULL
print(different_fruits)
## [[1]]
## [1] "mango"
##
## [[2]]
## [1] "watermelon"
##
## [[3]]
## [1] "Grapes"
#Combining List
different_items<-list("Guava","Berry")
combining_lists<-c(different_fruits,different_items)
print(combining_lists)
## [[1]]
## [1] "mango"
##
## [[2]]
## [1] "watermelon"
##
## [[3]]
## [1] "Grapes"
##
## [[4]]
## [1] "Guava"
##
## [[5]]
## [1] "Berry"
## [[1]]
## [1] "Fruits"
##
## [[2]]
## [1] 2
##
## [[3]]
## [1] FALSE
##
## [[4]]
## [1] "Fork" "Plate"
In R programming, vectors and lists are two fundamental data structures that serve distinct
purposes.
On the other hand, a list is a heterogeneous data structure that can hold elements of
different data types, including vectors, matrices, and even other lists. Lists are useful for
storing and manipulating complex data structures that require flexibility and versatility.
For example, a list of student information, where each element is a vector containing the
student's name, age, and grades.
42Mehul.R
Mehul Rajput
2024-09-20
results<-matrix(c(24,12,21,5,98,40),nrow=3, ncol=2)
print(results)
## [,1] [,2]
## [1,] 24 5
## [2,] 12 98
## [3,] 21 40
#adding rows
new_row<-c(78,90)
results<-rbind(results,new_row)
results
## [,1] [,2]
## 24 5
## 12 98
## 21 40
## new_row 78 90
#adding Columns
new_column<-c(23,67,54,90)
results<-cbind(results,new_column)
results
## new_column
## 24 5 23
## 12 98 67
## 21 40 54
## new_row 78 90 90
42Mehul.R
Mehul Rajput
2024-09-20
p_array<-array(20:50,dim=c(3,4,3))
p_array
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 20 23 26 29
## [2,] 21 24 27 30
## [3,] 22 25 28 31
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 32 35 38 41
## [2,] 33 36 39 42
## [3,] 34 37 40 43
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 44 47 50 22
## [2,] 45 48 20 23
## [3,] 46 49 21 24
## [1] 33
p_array
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 20 23 26 29
## [2,] 21 24 27 30
## [3,] 22 25 28 31
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 32 35 38 41
## [2,] 33 36 39 42
## [3,] 34 37 40 43
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 44 47 50 22
## [2,] 45 48 20 23
## [3,] 46 49 21 24
## [1] 44 45 46
p_array
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 20 23 26 29
## [2,] 21 24 27 30
## [3,] 22 25 28 31
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 32 35 38 41
## [2,] 33 36 39 42
## [3,] 34 37 40 43
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 44 47 50 22
## [2,] 45 48 20 23
## [3,] 46 49 21 24
#specific items
60%in%p_array
## [1] FALSE
#Dim,Length
dim(p_array)
## [1] 3 4 3
length(p_array)
## [1] 36
In R, an array is a data structure that stores a collection of elements, each
identified by an index or dimension. In R, arrays are similar to vectors, but they
can have multiple dimensions.
Here are some key characteristics of arrays in R:
Multi-dimensional: R arrays can have one or more dimensions, allowing
you to store and manipulate data in a tabular or matrix-like structure.
Homogeneous: All elements in an R array must be of the same data type,
such as numeric, character, or logical.
Fixed size: An R array has a fixed number of elements, which is determined
when the array is created.
42Mehul.R
Mehul Rajput
2024-09-20
#If condition
p<-25
j<-76
if(j>p){
print("j is superior than p")
}
#If Else
x<-24
y<-44
if(x>y){
print("x is smarter than y")
}else if(y>x){
print("y is smarter than x")
}
#Example
q<-0
w<-0
if(w>q){
print("Winner")
}else{
print("Loser")
}
## [1] "Loser"
#Nested Ifs
cricket<-90
football<-88
if(football>80){
if(football>100){
print("Great GOAl")
}else{
print("GOAL")
}
}else{
print("No GOAL")
}
## [1] "GOAL"
42Mehul.R
Mehul Rajput
2024-09-20
#While Loop
i <- 1
while (i <= 5) {
print(paste("Iteration", i))
i <- i + 1
}
#For Loop
sports<-c("Cricket","Football","Tennis","Badmintion")
for (sport in sports){
print(paste("I play",sport))
}
#Skipping Integration
for(i in 1:5){
if (i<-2){
next
}
print(paste("Iteration",i))
}
#Dice Example
for(i in 1:12){
roll<-sample(1:6,1)
print(paste("Roll",i,":",roll))
}