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

IDS Lab Manual

The document is a lab manual for R programming, detailing exercises on setting up the R environment, basic programming concepts, operations on vectors and matrices, and data structures. Each exercise includes objectives, procedures, and expected results, guiding users through the installation of R, implementation of data types, and manipulation of data structures like lists and data frames. The manual serves as a comprehensive introduction to data science using R programming.

Uploaded by

ieeeprocess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IDS Lab Manual

The document is a lab manual for R programming, detailing exercises on setting up the R environment, basic programming concepts, operations on vectors and matrices, and data structures. Each exercise includes objectives, procedures, and expected results, guiding users through the installation of R, implementation of data types, and manipulation of data structures like lists and data frames. The manual serves as a comprehensive introduction to data science using R programming.

Uploaded by

ieeeprocess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

INTRODUCTION TO DATA SCIENCE

R – Programming Lab Manual

Ex. No. 1. Install R-Programming environment Date:


Objective:
To set up the R programming environment on a computer, install basic R
packages, and verify their functionality using the install.packages() command
in R.
Requirements:
1. Computer with internet access.
2. R software (latest version).
3. RStudio (optional, for a better IDE experience).
Procedure:
1. Step 1: Download R
Visit the official R website: https://cran.r-project.org/.
Select the appropriate version for your operating system
(Windows).
Download the installer.
2. Step 2: Install R
Run the downloaded installer.
Follow the installation wizard and select the default options.
Complete the installation.
3. Step 3: (Optional) Download RStudio
Visit the RStudio website: https://posit.co/download/rstudio-
desktop/.
Download the free desktop version of RStudio.
Install RStudio following the instructions.
4. Step 4: Open R Environment
Launch R or RStudio.
Verify the installation by typing version in the console and
pressing Enter.
5. Step 5: Install Basic Packages
Use the install.packages() command to install essential
packages like ggplot2, dplyr, and tidyverse.
Example commands:
install.packages("ggplot2")
install.packages("dplyr")
install.packages("tidyverse")
Step 6: Load and Verify Packages
 Use the library() command to load the installed packages.
Example:
library(ggplot2)
library(dplyr)
library(tidyverse)
Check if the packages load without errors.
# Install packages
install.packages("ggplot2")
install.packages("dplyr")
install.packages("tidyverse")
# Load packages
library(ggplot2)
library(dplyr)
library(tidyverse)
Result:
The result refers to the expected outcome after downloading, installing, and
setting up the R programming environment and installing packages. The
environment was successfully installed, and essential packages were
installed.
Ex. No. 2. Basics of R Programming Date:

Objective:

To understand and implement the basic concepts of R programming,


including data types, variables, and operators.

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.

You might also like