R Lab Programs
R Lab Programs
# Vectors
my_vector <- c(1, 2, 3, 4, 5)
print("Numeric Vector")
print(my_vector)
# Matrices
my_matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3,
byrow=TRUE)
print("Matrix")
print(my_matrix)
# Lists
my_list <- list(name = "John", age = 25, scores = c(80, 90, 95))
print("List")
print(my_list)
# Data Frames
my_data <- data.frame(name = c("John", "Jane", "Mike","George"),
age = c(25, 30, 35, 7), scores = c(80, 90, 85,90))
print("Data Frame")
print(my_data)
class(my_vector)
Output:
$age
[1] 25
$scores
[1] 80 90 95
# Variables
name <- "John"
age <- 25
score <- 95.5
# Constants
PI <- 3.14159
GRAVITY<-9.81
# Data Types
is_student <- TRUE
grades <- c(80, 90, 85)
student_info <- list(name = "John", age = 25, scores = grades)
Output:
Data type of 'name': character
Data type of 'age': double
Data type of 'score': double
Data type of 'is_Student': logical
Data type of 'grades': double
Data type of 'Student_Info': list
Constant Value of 'PI': 3.14159
Constant Value of 'GRAVITY': 9.81
#Conditional Operator
msg<-ifelse(area>10,"Large Area", "Small Area")
#usage
circle_result<-calculate_area() #using default values
square_result<-calculate_area(radius=4, shape='square')
#display result
cat("Circle Result","\n")
print(circle_result)
cat("Square Result","\n")
print(square_result)
Output:
Circle Result
$shape
[1] "circle"
$radius
[1] 5
$area
[1] 31.41593
$Msg
[1] "Large Area"
Square Result
$shape
[1] "square"
$radius
[1] 4
$area
[1] 8
$Msg
[1] "Small Area"
Output:
[1] "Quick Sort"
[1] "Before Sort"
[1] 5 2 8 3 1 9
[1] "After Sort"
[1] 1 2 3 5 8 9
[1] "Binary Search Tree"
[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
[1] 9
# Calculus Differentiation
differentiate<-diff(numbers)
cat("Differentiation (First Difference):", differentiate,"\n")
# Calculus Integration
integrate<-cumsum(numbers)
cat("Integration (Cummulative Sum):",integrate, "\n")
Output:
Original Vector: 1 2 3 4 5
Cumulative Sums: 1 3 6 10 15
Cumulative Products: 1 2 6 24 120
Minimum Value: 1
Maximum Value: 5
Derivative (Calculus): NA 1 1 1 1
Accessing Results:
Original Vector: 1 2 3 4 5
Cumulative Sums: 1 3 6 10 15
Cumulative Products: 1 2 6 24 120
Minimum Value: 1
Maximum Value: 5
Derivative (Calculus): NA 1 1 1 1
DEPT. OF COMPUTER SCIENCE Page No. _____
SHREE MEDHA DEGREE COLLEGE, BALLARI
6. Write an R program for finding stationary distribution of markanov
chains.
# Install and load the markovchain package
install.packages("markovchain")
library(markovchain)
Output:
State1 State2
# Create matrices
matrix1 <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
matrix2 <- matrix(c(5, 6, 7, 8), nrow = 2, ncol = 2)
Output:
Vector Addition:
579
Vector Subtraction:
-3 -3 -3
Vector Scalar Product:
246
Matrix Addition:
6 8
10 12
Matrix Subtraction:
-4 -4
-4 -4
Matrix Multiplication:
23 34
31 46
Matrix Determinant:
-2
Matrix Transpose:
1324
[1] Matrix Inverse:"
[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5
# Creating a histogram
hist(data, main = "Histogram", xlab = "Values", ylab = "Frequency")
# 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")
Output:
Employee Data:
Maximum Age: 35
Output:
Predicted Salaries:
1 2
139333.3 152500.0