1.
Write a R program for different types of data structures in R
# Vector
my_vector <- c(1, 2, 3, 4, 5)
print(my_vector)
# List
my_list <- list(name = "RIYAZ", age = 30, city = "MEERUT")
print(my_list)
# Matrix
my_matrix <- matrix(1:6, nrow = 2, ncol = 3)
print(my_matrix)
# Data Frame
my_df <- [Link](Name = c("ANAND", "BOBBY", "CHIRAG"),
Age = c(25, 30, 22))
print(my_df)
# Array
my_array <- array(1:12, dim = c(2, 3, 2))
print(my_array
# Factor
my_factor <- factor(c("High", "Low", "Medium", "High", "Low"))
print(my_factor
# DataFrame with time-series
date <- [Link](c("2023-01-01", "2023-01-02", "2023-01-03"))
value <- c(100, 110, 105)
df_time_series <- [Link](Date = date, Value = value)
print(df_time_series
2. Write a R program that include variables, constants, data type
# Variables
name <- "ARFATH"
age <- 20
height <- 165.5
is_student <- TRUE
# Constants
PI <- 3.14159265359
G <- 9.81
# Data Types
char_vector <- c("apple", "banana", "cherry")
int_vector <- c(1, 2, 3, 4, 5)
double_vector <- c(1.5, 2.7, 3.0)
logical_vector <- c(TRUE, FALSE, TRUE, FALSE
#Print variables, constants, and data types
cat("Name:", name, "\n")
cat("Age:", age, "\n")
cat("Height:", height, "\n")
cat("Is Student:", is_student, "\n")
cat("PI Constant:", PI, "\n")
cat("Character Vector:", char_vector, "\n")
cat("Integer Vector:", int_vector, "\n")
cat("Double Vector:", double_vector, "\n")
cat("Logical Vector:", logical_vector, "\n")
3. Write a R program that include different operators, control structures,
default values for arguments, returning complex objects.
# Function with default argument values
calculate_area <- function(shape = "circle", radius = 1, length = 1, width = 1)
{
if (shape == "circle")
{
area <- pi * radius^2
}
else if (shape == "rectangle")
{
area <- length * width
}
else
{
area <- 0
}
return(list(shape = shape, area = area))
}
# Calculate areas using the function
circle_area <- calculate_area("circle", radius = 5)
rect_area <- calculate_area("rectangle", length = 4, width = 6)
default_area <- calculate_area()
# Print the results
cat("Circle Area:", circle_area$area, "for shape:", circle_area$shape, "\n")
cat("Rectangle Area:", rect_area$area, "for shape:", rect_area$shape, "\n")
cat("Default Area:", default_area$area, "for shape:", default_area$shape, "\n")
# Conditional statements grade <- 85
if (grade >= 90)
{ cat("A\n") }
else if (grade >= 80)
{ cat("B\n") }
else if (grade >= 70)
{ cat("C\n") }
else { cat("F\n")
}
#loop
for(I in 1:5)
{
cat(“iteration:”,I,”\n”)
}
#logical operators
Is_sunny<-TRUE
Is_warm<-TRUE
If (is_sunny && is_warm) {
cat("It's a nice day!\n")
}
# Complex objects (list of lists)
student1 <- list(name = "Aliya", age = 25)
student2 <- list(name = "Babar", age = 22)
students <- list(student1, student2)
# Accessing complex object elements
cat("First student's name:", students[[1]]$name, "\n")
cat("Second student's age:", students[[2]]$age, "\n")
4. Quick sort program
# Quick Sort function
quick_sort <- function(arr)
{
if (length(arr) <= 1)
{
return(arr)
}
pivot <- arr[1]
less <- arr[arr < pivot]
equal <- arr[arr == pivot]
greater <- arr[arr > pivot]
return(c(quick_sort(less), equal, quick_sort(greater)))
}
# Example usage
unsorted_array <- c(9, 7, 5, 11, 12, 2, 14, 3, 10, 6)
sorted_array <- quick_sort(unsorted_array)
cat("QUICK SORT is in Sorted Array :", sorted_array, "\n")
#Binary search tree
# Define a Node structure for the Binary Search Tree
Node <- function(key)
{ return(list(key = key, left = NULL, right = NULL))
}
# Insert a value into the BST
insert <- function(root, key)
{
if ([Link](root))
{ return(Node(key))
}
if (key < root$key)
{ root$left <- insert(root$left, key)
}
else if (key > root$key)
{ root$right <- insert(root$right, key)
}
return(root)
}
# Example usage
bst_root <- NULL
bst_root <- insert(bst_root, 10)
bst_root <- insert(bst_root, 5)
bst_root <- insert(bst_root, 15)
bst_root <- insert(bst_root, 3)
bst_root <- insert(bst_root, 7)
bst_root <- insert(bst_root, 12)
bst_root <- insert(bst_root, 18)
cat("BINARY SEARCH TREE >>>In-order Traversal (Sorted Order): ")
inorder_traversal(bst_root)
Write a R program for any visual representation of an object with
creating graphs using graphic functions: Plot(), Hist(), Linechart(), Pie(),
Boxplot(), Scatter plots().
# Creating a vector of data
data <- c(10, 15, 20, 25, 30, 35, 40, 45, 50)
# Plotting a line chart
plot(data, type = "l", main = "Line Chart", xlab = "X-axis", ylab = "Y-axis")
# Creating a histogram
hist(data, main = "Histogram", xlab = "Values", ylab = "Frequency")
# Creating a pie chart
categories<-c("A", "B", "C")
values<-c(30, 40, 50)
pie(values, labels=categories, main = "Pie Chart", col=c("red", "green",
"blue"))
# Creating a boxplot
data<-list(A=c(2, 4, 6, 8), B=c(1, 3, 5, 7))
boxplot(data, main = "Boxplot", xlab="Groups", ylab = "Values")
# Creating a scatter plot
x <- c(1, 2, 3, 4, 5)
y <- c(10, 20, 30, 20, 50)
plot(x, y, pch=19, col="blue", main = "Scatter Plot", xlab = "X-axis", ylab =
"Y-axis")