0% found this document useful (0 votes)
3 views12 pages

Assignment 3 - Merged

The document provides a comprehensive overview of R programming concepts, focusing on data structures such as vectors, lists, matrices, and arrays. It includes examples of manipulating these structures, as well as control flow mechanisms like if conditions and loops. The document emphasizes the differences between homogeneous and heterogeneous data types and illustrates their applications in R.

Uploaded by

green apple
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Assignment 3 - Merged

The document provides a comprehensive overview of R programming concepts, focusing on data structures such as vectors, lists, matrices, and arrays. It includes examples of manipulating these structures, as well as control flow mechanisms like if conditions and loops. The document emphasizes the differences between homogeneous and heterogeneous data types and illustrates their applications in R.

Uploaded by

green apple
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

42Mehul.

R
Mehul Rajput

2024-09-20
p_vector<-c("apple", "banana", "grapes", "cheery", "watermelon")
p_vector

## [1] "apple" "banana" "grapes" "cheery"


"watermelon"

sorting_vector<-sort(p_vector)
sorting_vector

## [1] "apple" "banana" "cheery" "grapes"


"watermelon"

(p_vector[1])

## [1] "apple"

(p_vector[5])

## [1] "watermelon"

(p_vector[c(1,3,5)])

## [1] "apple" "grapes" "watermelon"

p_vector[3]<-"mango"
p_vector

## [1] "apple" "banana" "mango" "cheery"


"watermelon"
42Mehul.R
Mehul Rajput

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"

#Different Data Types


data_items<-list("Fruits",2,FALSE,c("Fork","Plate"))
print(data_items)

## [[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.

A vector is a single-dimensional, homogeneous data structure, meaning it can only hold


elements of the same data type, such as numeric, character, or logical. Vectors are useful for
storing and manipulating data that requires a specific order or sequence. For example, a
vector of exam scores or a vector of names.

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

#Accessing second row,first column,2nd Matrix


p_array[2,1,2]

## [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

#Accessing all rows of 1st column,3rd Matrix


p_array[,1,3]

## [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")
}

## [1] "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")
}

## [1] "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
}

## [1] "Iteration 1"


## [1] "Iteration 2"
## [1] "Iteration 3"
## [1] "Iteration 4"
## [1] "Iteration 5"

#For Loop
sports<-c("Cricket","Football","Tennis","Badmintion")
for (sport in sports){
print(paste("I play",sport))
}

## [1] "I play Cricket"


## [1] "I play Football"
## [1] "I play Tennis"
## [1] "I play Badmintion"

#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))
}

## [1] "Roll 1 : 4"


## [1] "Roll 2 : 2"
## [1] "Roll 3 : 6"
## [1] "Roll 4 : 3"
## [1] "Roll 5 : 4"
## [1] "Roll 6 : 5"
## [1] "Roll 7 : 3"
## [1] "Roll 8 : 1"
## [1] "Roll 9 : 6"
## [1] "Roll 10 : 6"
## [1] "Roll 11 : 3"
## [1] "Roll 12 : 2"

You might also like