0% found this document useful (0 votes)
15 views

Udit R Programing

Research on R programming can cover a wide range of topics, given R's versatility and popularity in data analysis, statistical computing, and visualization. Here are some potential areas of research: Statistical Methods: Investigate advanced statistical methods and algorithms implemented in R, such as machine learning techniques, time series analysis, Bayesian inference, or spatial statistics. Explore their applications in various domains like finance, healthcare, or social sciences. Data Vis

Uploaded by

Udit Dumka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Udit R Programing

Research on R programming can cover a wide range of topics, given R's versatility and popularity in data analysis, statistical computing, and visualization. Here are some potential areas of research: Statistical Methods: Investigate advanced statistical methods and algorithms implemented in R, such as machine learning techniques, time series analysis, Bayesian inference, or spatial statistics. Explore their applications in various domains like finance, healthcare, or social sciences. Data Vis

Uploaded by

Udit Dumka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lab Record File

of

R Programming
(CSF341)

BACHELOR OF TECHNOLOGY
In

COMPUTER SCIENCE AND


ENGINEERING

Session
2023-2024
5th Sem 3rd
year

Submitted to: - Submitted by: -


Dr. Somil Kumar Gupta Name: Dherendra Singh
Assistant Professor Roll No: 210102354
(School of Computing) Sap ID:
1000016569
(Section: B - P1)

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.

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 1: Write a R program to explain basic syntax.

Code:

# Comments are preceded by the '#' symbol and are ignored by the interpreter

# Assigning values to variables


x <- 10
y <- 5

# Performing arithmetic operations


sum_result <- x + y
diff_result <- x - y
product_result <- x * y
quotient_result <- x / y

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

Dherendra Singh SAP ID:1000016569 B (P1)


Dherendra Singh SAP ID:1000016569 B (P1)
Experiment 2: Write a R program using statements. (If Statement, Else If, If Else, Nested If Statements,
Switch statement, Next statement, Break statement).

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

Dherendra Singh SAP ID:1000016569 B (P1)


for (i in 1:5) {
if (i == 3) {
next # Skip the rest of the loop body for i == 3
}
cat("Iteration:", i, "\n")
}

# Break Statement
for (i in 1:5) {
if (i == 3) {
break # Exit the loop when i == 3
}
cat("Iteration:", i, "\n")
}

Output:

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 3: Write a R program using loops. (For, while and repeat loops)

Code:

# For Loop
cat("For Loop:\n")
for (i in 1:5) {
cat("Iteration:", i, "\n")
}

# While Loop cat("\


nWhile Loop:\n") j <-
1
while (j <= 5) {
cat("Iteration:", j, "\n")
j <- j + 1
}

# Repeat Loop with Break


cat("\nRepeat Loop:\n")
k <- 1
repeat {
cat("Iteration:", k, "\n")
k <- k + 1
if (k > 5) {
break # Exit the loop when k is greater than 5
}
}

Output:

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 4: Write a R program using user defined and built-in functions.

Code:
# User-defined function
calculate_square <- function(num) {
result <- num^2
return(result)
}

# Built-in function
x <- 5
sqrt_result <- sqrt(x)

# Print the square of a number using the user-defined function


cat("Square of", x, "using user-defined function:", calculate_square(x), "\n")

# Print the square root of a number using the built-in function


cat("Square root of", x, "using built-in function:", sqrt_result, "\n")

# Combining user-defined and built-in functions


sum_of_squares <- function(a, b) {
square_of_a <- calculate_square(a)
square_of_b <- calculate_square(b)
return(square_of_a + square_of_b)
}

# Using the combined function


result <- sum_of_squares(3, 4)
cat("Sum of squares of 3 and 4:", result, "\n")

Output:

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 5: Write a R program for the implementation of data structures (Vectors, Lists,
Matrices).

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)

# Access elements in the list


print("Accessing elements in the list:")
print("Numbers:")
print(my_list$numbers)

# Matrices
my_matrix <- matrix(1:9, nrow = 3, ncol = 3)

# Print matrix
print("Matrix:")
print(my_matrix)

# Access elements in the matrix


print("Accessing elements in the matrix:")
print("Element in row 2, column 3:")
print(my_matrix[2, 3])

# Modify elements in the matrix

Dherendra Singh SAP ID:1000016569 B (P1)


print("Modifying elements in the matrix:")
my_matrix[2, 3] <- 99
print(my_matrix)

Output:

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 6: Write a R program for the implementation of data structures (Arrays, Data
frames, Factors).
Code:

# Arrays
my_array <- array(1:12, dim = c(2, 3, 2))

# Print array
print("Array:")
print(my_array)

# Access elements in the array


print("Accessing elements in the array:")
print("Element in position (1, 2, 2):")
print(my_array[1, 2, 2])

# Data Frames
name <- c("John", "Jane", "Bob")
age <- c(25, 30, 22)
city <- c("New York", "Los Angeles", "Chicago")

# Create data frame


my_data_frame <- data.frame(Name = name, Age = age, City = city)

# Print data frame


print("Data Frame:")
print(my_data_frame)

# Access columns in the data frame


print("Accessing columns in the data
frame:") print("Names:")
print(my_data_frame$Name)

# Factors
gender <- c("Male", "Female", "Male")

# Create factor
gender_factor <- factor(gender, levels = c("Male", "Female"))

# Print factor
print("Factor:")
print(gender_factor)

# Access levels in the factor


print("Accessing levels in the factor:")
print(levels(gender_factor))

Dherendra Singh SAP ID:1000016569 B (P1)


Output:

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 7: Write a R program to import external data from various file formats
Code:

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 8: Write a R program for the implementation of data statistical functions.
Code:

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

# Minimum and Maximum


min_value <- min(data)
max_value <- max(data)
print(paste("Minimum:", min_value))
print(paste("Maximum:", max_value))

# Quantiles
quantiles <- quantile(data)
print("Quantiles:")
print(quantiles)

# Correlation matrix (example with a matrix)


matrix_data <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3)
correlation_matrix <- cor(matrix_data)
print("Correlation Matrix:")
print(correlation_matrix)

Dherendra Singh SAP ID:1000016569 B (P1)


Output:

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 9: Write a R program for to implement graphics using plot, line, scatterplot.

Code:

# Generate some sample data


x <- 1:10
y1 <- x^2
y2 <- 2 * x

# Create a line plot


plot(x, y1, type = "l", col = "blue", lwd = 2, main = "Line Plot", xlab = "X-axis", ylab = "Y-axis")

# Add a second line to the plot


lines(x, y2, col = "red", lty = 2, lwd = 2)

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

# Add points to the scatter plot


points(x, y2, col = "red", pch = 18)

# Add a legend for the scatter plot


legend("topright", legend = c("y = x^2", "y = 2x"), col = c("blue", "red"), pch = c(16, 18))

# Save the plots to a file (optional)


# dev.copy(png, "plot.png")
# dev.off()

Output:

Dherendra Singh SAP ID:1000016569 B (P1)


Experiment 10: Write a R program for to implement graphics using pie charts and bars.
Code:

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

# Save the plots to a file (optional)


# dev.copy(png, "pie_chart.png")
# dev.off()

# dev.copy(png, "bar_chart.png")
# dev.off()

Output:

Dherendra Singh SAP ID:1000016569 B (P1)

You might also like