Udit R Programing
Udit R Programing
of
R Programming
(CSF341)
BACHELOR OF TECHNOLOGY
In
Session
2023-2024
5th Sem 3rd
year
SCHOOL OF
COMPUTING
DIT
UNIVERSITY,
DEHRADUN
(State Private University through State Legislature Act No. 10 of 2013 of Uttarakhand
and approved by UGC)
Mussoorie Diversion Road, Dehradun, Uttarakhand - 248009, India.
Code:
# Comments are preceded by the '#' symbol and are ignored by the interpreter
# Printing results
cat("Sum:", sum_result, "\n")
cat("Difference:", diff_result, "\n")
cat("Product:", product_result, "\n")
cat("Quotient:", quotient_result, "\n")
# Conditional statements
if (x > y) {
cat("x is greater than y\n")
} else if (x < y) {
cat("x is less than y\n")
} else {
cat("x is equal to y\n")
}
# Loops
for (i in 1:5) {
cat("Iteration:", i, "\n")
}
# Functions
calculate_square <- function(num) {
return(num^2)
}
# Using a function
result <- calculate_square(4)
cat("Square of 4:", result, "\n")
Output:
Code:
# If Statement
x <- 10
if (x > 0) {
cat("x is positive.\n")
}
# If Else Statement
y <- -5
if (y > 0) {
cat("y is positive.\n")
} else {
cat("y is non-positive.\n")
}
# Else If Statement
z <- 0
if (z > 0) {
cat("z is positive.\n")
} else if (z < 0) {
cat("z is negative.\n")
} else {
cat("z is zero.\n")
}
# Nested If Statements
a <- 15
b <- 10
if (a > 0) {
if (b > 0) {
cat("Both a and b are positive.\n")
} else {
cat("Only a is positive.\n")
}
} else {
cat("Both a and b are non-positive.\n")
}
# Switch Statement
day <- "Monday"
switch(day,
"Monday" = cat("It's the start of the week.\n"),
"Friday" = cat("It's almost the weekend!\n"),
cat("It's a regular day.\n")
)
# Next Statement
# Break Statement
for (i in 1:5) {
if (i == 3) {
break # Exit the loop when i == 3
}
cat("Iteration:", i, "\n")
}
Output:
Code:
# For Loop
cat("For Loop:\n")
for (i in 1:5) {
cat("Iteration:", i, "\n")
}
Output:
Code:
# User-defined function
calculate_square <- function(num) {
result <- num^2
return(result)
}
# Built-in function
x <- 5
sqrt_result <- sqrt(x)
Output:
Code:
# Vectors
numeric_vector <- c(1, 2, 3, 4, 5)
character_vector <- c("apple", "banana", "orange")
logical_vector <- c(TRUE, FALSE, TRUE)
# Print vectors
print("Numeric Vector:")
print(numeric_vector)
print("Character Vector:")
print(character_vector)
print("Logical Vector:")
print(logical_vector)
# Lists
my_list <- list(
numbers = numeric_vector,
fruits = character_vector,
flags = logical_vector
)
# Print list
print("List:")
print(my_list)
# Matrices
my_matrix <- matrix(1:9, nrow = 3, ncol = 3)
# Print matrix
print("Matrix:")
print(my_matrix)
Output:
# Arrays
my_array <- array(1:12, dim = c(2, 3, 2))
# Print array
print("Array:")
print(my_array)
# Data Frames
name <- c("John", "Jane", "Bob")
age <- c(25, 30, 22)
city <- c("New York", "Los Angeles", "Chicago")
# Factors
gender <- c("Male", "Female", "Male")
# Create factor
gender_factor <- factor(gender, levels = c("Male", "Female"))
# Print factor
print("Factor:")
print(gender_factor)
# Sample data
data <- c(23, 45, 67, 12, 56, 34, 89, 67, 43, 21)
# Mean
mean_value <- mean(data)
print(paste("Mean:", mean_value))
# Median
median_value <- median(data)
print(paste("Median:", median_value))
# Standard Deviation
sd_value <- sd(data)
print(paste("Standard Deviation:", sd_value))
# Variance
var_value <- var(data)
print(paste("Variance:", var_value))
# Sum
sum_value <- sum(data)
print(paste("Sum:", sum_value))
# Quantiles
quantiles <- quantile(data)
print("Quantiles:")
print(quantiles)
Code:
# Add a legend
legend("topright", legend = c("y = x^2", "y = 2x"), col = c("blue", "red"), lty = c(1, 2), lwd = 2)
# Scatter plot
plot(x, y1, col = "blue", pch = 16, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
Output:
# Sample data
categories <- c("Category A", "Category B", "Category C", "Category D")
values <- c(30, 20, 15, 35)
# Pie chart
pie(values, labels = categories, main = "Pie Chart", col = rainbow(length(categories)), cex = 0.8)
# Bar chart
barplot(values, names.arg = categories, main = "Bar Chart", col = rainbow(length(categories)), xlab =
"Categories", ylab = "Values")
# dev.copy(png, "bar_chart.png")
# dev.off()
Output: