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

R programming lab programs

The document contains a series of R programming lab exercises covering various topics such as data structures, variables, operators, control structures, cumulative calculations, and linear algebra operations. Each section includes example code and output demonstrating the functionality of R for tasks like creating vectors, matrices, and performing mathematical operations. The exercises aim to provide hands-on experience with R programming concepts.

Uploaded by

Vikas Tuwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

R programming lab programs

The document contains a series of R programming lab exercises covering various topics such as data structures, variables, operators, control structures, cumulative calculations, and linear algebra operations. Each section includes example code and output demonstrating the functionality of R for tasks like creating vectors, matrices, and performing mathematical operations. The exercises aim to provide hands-on experience with R programming concepts.

Uploaded by

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

R Programming Lab Programs

1) Write a R program for different types of data structures in R.

# Vector
vector_example<- c(1, 2, 3, 4, 5)
print("Vector:")
print(vector_example)

# List
list_example<- list(name = "John", age = 30, city = "New York")
print("List:")
print(list_example)

# Matrix
matrix_example<- matrix(1:9, nrow = 3, ncol = 3)
print("Matrix:")
print(matrix_example)

# Data Frame
data_frame_example<- data.frame(
name = c("John", "Alice", "Bob"),
age = c(30, 25, 35),
city = c("New York", "Los Angeles", "Chicago")
)
print("Data Frame:")
print(data_frame_example)
# Factor
factor_example<- factor(c("Male", "Female", "Male", "Female", "Male"))
print("Factor:")
print(factor_example)

# Array
array_example<- array(1:12, dim = c(3, 2, 2))
print("Array:")
print(array_example)

output:-
[1] Vector:
[1] 1 2 3 4 5

[1] List:
$name
[1] "John"
$age
[1] 30
$city
[1] New York

[1] Matrix:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] Data Frame:
name age city
1 John 30 New York
2 Alice 25 Los Angeles
3 Bob 35 Chicago

[1] Factor:
[1] Male Female Male Female Male
Levels: Female Male

[1] Array:
[ , , 1]
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6

[ , , 2]
[,1] [,2]
[1,] 7 10
[2,] 8 11
[3,] 9 12
2) Write a R program that include variables, constants, and data types.

# Constants
PI <- 3.14159
GRAVITY <- 9.81

# Variables
name <- "John"
age <- 30
height <- 175.5
is_student<- TRUE

# Printing constants and variables


print("Constants:")
print(PI)
print(GRAVITY)

print("Variables:")
print(name)
print(age)
print(height)
print(is_student)

# Data types
print("Data types:")
print(typeof(name)) # Character
print(typeof(age)) # Integer
print(typeof(height)) # Double
print(typeof(is_student)) # Logical

Output:-
[1] Constants:
[1] 3.14159
[1] 9.81
[1] Variables:
[1] "John"
[1] 30
[1] 175.5
[1] TRUE
[1] "Data types:"
[1] "character"
[1] "double"
[1] "double"
[1] "logical"
3) Write a R program that include different operators, control structures,
default values for arguments, returning complex objects.

# Function to calculate the area and perimeter of a rectangle


calculate_rectangle<- function(length, width = 1)
{
# Calculate area
area <- length * width

# Calculate perimeter
perimeter <- 2 * (length + width)

# Return a list containing area and perimeter


return(list(area = area, perimeter = perimeter))
}
# Function to determine if a number is even or odd
even_or_odd<- function(x)
{
if (x %% 2 == 0)
{
result <- "Even"
}
else
{
result <- "Odd"
}
return(result)
}
# Main code
# Calculate area and perimeter of a rectangle with default width
rectangle1 <- calculate_rectangle(length = 5)
print("Rectangle 1:")
print(rectangle1)

# Calculate area and perimeter of a rectangle with specified width


rectangle2 <- calculate_rectangle(length = 7, width = 3)
print("Rectangle 2:")
print(rectangle2)

# Check if a number is even or odd


number1 <- 10
number2 <- 7
print(paste(number1, "is", even_or_odd(number1)))
print(paste(number2, "is", even_or_odd(number2)))

Output:-
[1] Rectangle 1:
$area
[1] 5
$perimeter
[1] 12
[1] Rectangle 2:
$area
[1] 21
$perimeter
[1] 20
[1] "10 is Even"
[1] "7 is Odd"
4) Write a R program for calculating cumulative sums, and products
minima maxima and calculus.

# Function to calculate cumulative sum


calculate_cumulative_sum<- function(numbers)
{
cum_sum<- cumsum(numbers)
return(cum_sum)
}
# Function to calculate cumulative product
calculate_cumulative_product<- function(numbers)
{
cum_prod<- cumprod(numbers)
return(cum_prod)
}
# Function to find minimum value
find_minimum<- function(numbers)
{
min_val<- min(numbers)
return(min_val)
}
# Function to find maximum value
find_maximum<- function(numbers)
{
max_val<- max(numbers)
return(max_val)
}
# Function to perform calculus (e.g., differentiate)
perform_calculus<- function(func, x, h = 1e-6)
{
derivative <- (func(x + h) - func(x)) / h
return(derivative)
}
# Example function for calculus
# Define a simple quadratic function
quadratic_function<- function(x)
{
return(x^2 + 2*x + 1)
}
# Main code
# Sample numeric vector
numbers <- c(1, 2, 3, 4, 5)
# Calculate cumulative sum
cumulative_sum<- calculate_cumulative_sum(numbers)
print("Cumulative Sum:")
print(cumulative_sum)
# Calculate cumulative product
cumulative_product<- calculate_cumulative_product(numbers)
print("Cumulative Product:")
print(cumulative_product)
# Find minimum value
minimum <- find_minimum(numbers)
print("Minimum:")
print(minimum)
# Find maximum value
maximum <- find_maximum(numbers)
print("Maximum:")
print(maximum)
# Perform calculus (differentiate) on the quadratic function at x = 2
derivative <- perform_calculus(quadratic_function, x = 2)
print("Derivative of the quadratic function at x = 2:")
print(derivative)

Output:-
[1] Cumulative Sum:
[1] 1 3 6 10 15
[1] Cumulative Product:
[1] 1 2 6 24 120
[1] Minimum:
[1] 1
[1] Maximum:
[1] 5
[1] Derivative of the quadratic function at x = 2:
[1] 6.000001
5) Write a R program that include linear algebra operations on vectors
and matrices.

# Define two sample vectors


vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)
# Define two sample matrices
matrix1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)
matrix2 <- matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Function to perform vector addition
vector_addition<- function(v1, v2)
{
result <- v1 + v2
return(result)
}
# Function to perform vector addition
vector_subtraction<- function(v1, v2)
{
result <- v1 - v2
return(result)
}
# Function to perform matrix addition
matrix_addition<- function(m1, m2)
{
result <- m1 + m2
return(result)
}
# Function to perform matrix subtraction
matrix_subtraction<- function(m1, m2)
{
result <- m1 - m2
return(result)
}
# Function to perform vector dot product
vector_dot_product<- function(v1, v2)
{
result <- sum(v1 * v2)
return(result)
}
# Function to perform matrix multiplication
matrix_multiplication<- function(m1, m2)
{
result <- m1 * m2
return(result)
}
# Function to calculate the transpose of a matrix
matrix_transpose<- function(m)
{
result <- t(m)
return(result)
}
# Main code
# Perform vector addition
print("Vector Addition:")
print(vector_addition(vector1, vector2))

# Perform vector subtraction


print("Vector Subtraction:")
print(vector_subtraction(vector1, vector2))

# Perform matrix addition


print("Matrix Addition:")
print(matrix_addition(matrix1, matrix2))

# Perform matrix subtraction


print("Matrix Subtraction:")
print(matrix_subtraction(matrix1, matrix2))

# Perform vector dot product


print("Vector Dot Product:")
print(vector_dot_product(vector1, vector2))

# Perform matrix multiplication


print("Matrix Multiplication:")
print(matrix_multiplication(matrix1, matrix2))
# Calculate the transpose of a matrix
print("Transpose of Matrix1:")
print(matrix_transpose(matrix1))

Output:-
[1] Vector Addition:
[1] 5 7 9

[1] Vector Subtraction:


[1] -3 -3 -3

[1] Matrix Addition:


[,1] [,2] [,3]
[1,] 8 11 14
[2,] 12 15 18

[1] Matrix Subtraction:


[,1] [,2] [,3]
[1,] -6 -7 -8
[2,] -4 -5 -6

[1] Vector Dot Product:


[1] 32
[1] Matrix Multiplication:
[,1] [,2] [,3]
[1,] 7 18 33
[2,] 32 50 72

[1] Transpose of Matrix1:


[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6

You might also like