IDS Lab Manual
IDS Lab Manual
Objective:
Procedure:
Step1 - Data Types
num <- 42
int <- as.integer(42)
char <- "Hello, R!"
logi <- TRUE
comp <- 3 + 2i
Step 2 - Variables
x <- 10
y <- "Data Science"
z <- TRUE
Step 3 - Arithmetic Operators
a <- 5 + 3
b <- 5 - 2
c <- 5 * 2
d <- 5 / 2
Step 4 - Relational Operators
e <- 5 > 3
f <- 5 == 3
Step 5 - Logical Operators
g <- (5 > 3) & (3 < 5)
h <- (5 > 3) | (3 > 5)
Step 6 - Built-in Functions
sqrt_val <- sqrt(16)
abs_val <- abs(-5)
fact <- factorial(5)
print(num)
print(int)
print(char)
print(logi)
print(comp)
print(x)
print(y)
print(z)
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(sqrt_val)
print(abs_val)
print(fact)
OUTPUT:
Data Types Output:
Numeric: 42
Integer: 42
Character: "Hello, R!"
Logical: TRUE
Complex: 3 + 2i
Arithmetic Operators Output:
Addition: 8
Subtraction: 3
Multiplication: 10
Division: 2.5
Relational and Logical Operators Output:
Relational: TRUE, FALSE
Logical: TRUE, TRUE
Built-in Functions Output:
Square root: 4
Absolute value: 5
Factorial: 120
Result:
Successfully learned and implemented basic concepts of R programming,
including data types, variables, operators, and built-in functions.
Ex. No. 3. Operations on Vectors and Matrices in R Date:
Objective:
To illustrate basic arithmetic operations on vectors and matrices in R using
commands for summation, subtraction, multiplication, and division.
Procedure:
Step 1: Vector Subsetting
vec <- c(10, 20, 30, 40, 50)
subset_1 <- vec[1]
subset_range <- vec[2:4]
subset_condition <- vec[vec > 20]
Step 2: Matrix Subsetting
mat <- matrix(1:9, nrow = 3, byrow = TRUE)
subset_row <- mat[1, ]
subset_col <- mat[, 2]
subset_element <- mat[2, 3]
Step 3: Array Creation
arr <- array(1:27, dim = c(3, 3, 3))
print("Vector Subsetting:")
print(subset_1)
print(subset_range)
print(subset_condition)
print("Matrix Subsetting:")
print(subset_row)
print(subset_col)
print(subset_element)
print("3×3 Array with Three Matrices:")
print(arr)
Output:
1. Vector Operations:
Summation: c(3, 6, 9, 12)
Subtraction: c(1, 2, 3, 4)
Multiplication: c(2, 8, 18, 32)
Division: c(2, 2, 2, 2)
2. Matrix and Vector Multiplication:
[1,] 28
[2,] 64
[3,] 100
3. Matrix and Vector Division (Element-wise):
[1,] 0.5 1.0 1.5
[2,] 1.0 1.5 2.0
[3,] 1.5 2.0 2.5
Results:
Successfully performed summation, subtraction, multiplication, and division
operations on vectors, as well as multiplication and division operations
between matrices and vectors.
Ex. No. 4. Vector and Matrix Subsetting and Array Creation in R Date:
Objective:
To understand and implement subsetting techniques on vectors and matrices
in R, and to create a 3×3 array with three 3×3 matrices.
Procedure:
Step 1: Vector Subsetting
vec <- c(10, 20, 30, 40, 50)
subset_1 <- vec[1]
subset_range <- vec[2:4]
subset_condition <- vec[vec > 20]
Step 2: Matrix Subsetting
mat <- matrix(1:9, nrow = 3, byrow = TRUE)
subset_row <- mat[1, ]
subset_col <- mat[, 2]
subset_element <- mat[2, 3]
# Step 3: Array Creation
arr <- array(1:27, dim = c(3, 3, 3))
print("Vector Subsetting:")
print(subset_1)
print(subset_range)
print(subset_condition)
print("Matrix Subsetting:")
print(subset_row)
print(subset_col)
print(subset_element)
print("3×3 Array with Three Matrices:")
print(arr)
Output:
1. Vector Subsetting:
First Element: 10
Range of Elements: 20, 30, 40
Elements Greater Than 20: 30, 40, 50
2. Matrix Subsetting:
First Row: 1, 2, 3
Second Column: 2, 5, 8
Element at Row 2, Column 3: 6
3. 3×3 Array Output:
Example of an array with three 3×3 matrices:
,,1
1 2 3
4 5 6
7 8 9
,,2
10 11 12
13 14 15
16 17 18
,,3
19 20 21
22 23 24
25 26 27
Results:
Successfully implemented vector and matrix subsetting and created a 3×3
array with three matrices.
Ex. No. 5. Implementation of Data Structures in R Date:
Objective:
To understand and implement various data structures in R, including vectors,
lists, and data frames, along with their operations.
Procedure:
Step 1: Vectors
vec <- c(10, 20, 30, 40, 50)
first_element <- vec[1]
range_elements <- vec[2:4]
vec_sum <- sum(vec)
vec_mean <- mean(vec)
Step 2: Lists
lst <- list(name = "John", age = 25, scores = c(80, 90, 85))
name <- lst$name
age <- lst$age
scores <- lst$scores
Step 3: Data Frames
df <- data.frame(
ID = c(1, 2, 3),
Name = c("Alice", "Bob", "Charlie"),
Age = c(24, 27, 22),
Score = c(85, 90, 88)
)
first_row <- df[1, ]
age_column <- df$Age
subset_df <- df[df$Age > 23, ]
print("Vector Operations:")
print(first_element)
print(range_elements)
print(vec_sum)
print(vec_mean)
print("List Operations:")
print(name)
print(age)
print(scores)
print("Data Frame Operations:")
print(first_row)
print(age_column)
print(subset_df)
Output:
Vector Operations:
First Element: 10
Range of Elements: 20, 30, 40
Sum of Vector: 150
Mean of Vector: 30
List Operations:
Name: "John"
Age: 25
Scores: 80, 90, 85
Data Frame Operations:
First Row
ID Name Age Score
1 Alice 24 85
Age Column: 24, 27, 22
Subset of Data Frame (Age > 23)
ID Name Age Score
1 Alice 24 85
2 Bob 27 90
Result:
Successfully implemented various data structures in R, including vectors,
lists, and data frames, and demonstrated operations on them.