BDA Lab Record
BDA Lab Record
1
21311A05L9 Kalyan. K
2
21311A05L9 Kalyan. K
3
21311A05L9 Kalyan. K
3. Write R command to
4
21311A05L9 Kalyan. K
5
21311A05L9 Kalyan. K
A <- c(1, 2, 3)
B <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3)
# Multiply vector A by matrix B
C <- A %*% B
# Output the result C
Output: 14 32 50
A <- c(1, 2, 3)
B <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2)
# Multiply row vector B by matrix A
C <- A %*% B
# Output the result C
Output:14 32
A <- c(1, 2)
B <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
# Multiply vector A by matrix B
C <- A %*% B
# Output the result C
Output: 5 11
A <- c(1, 2)
B <- matrix(c(1, 2, 3, 4,5,6), nrow = 2, ncol = 3)
# Multiply vector A by matrix B
C <- A %*% B
# Output the result C
Output: 5 11 17
6
21311A05L9 Kalyan. K
[,1] [,2]
2 6
4 8
7
21311A05L9 Kalyan. K
4. Write R command to
v <- c(1, 2, 3, 4, 5)
v[v > 3]
Output: 4 5
v <- c(1, 2, 3, 4, 5)
v[v < 3]
Output: 1 2
v <- c(1, 2, 3, 4, 5)
v[c(1, 3, 5)]
Output: 1 3 5
v <- c(1, 2, 3, 4, 5)
v[-c(2, 4)]
Output: 1 3 5
8
21311A05L9 Kalyan. K
Output: 6
9
21311A05L9 Kalyan. K
Example 2:
# Create an array of 3x3 matrices my_array <-
array(0, dim = c(3, 3, 3))
# View the array
my_array
Output:
10
21311A05L9 Kalyan. K
# Output:
,,1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
get_person_info(person2)
Output:
11
21311A05L9 Kalyan. K
The R command to create a tshirt_factor variable with ordered levels 'S', 'M', and 'L':
tshirt_factor<- factor(c("S", "M", "L"), ordered = TRUE, levels = c("S", "M", "L")) tshirt_factor
Output:
[1] S M L
ii) Write the command in R console to create a new data frame containing the ‗age‘
parameter from the existing data frame. Check if the result is a data frame or not. Also R
commands for data frame functions cbind(), rbind(), sort()
Print (Student)
#create a new data frame containing the ‘age’ parameter from the existing data frame.
Student_df=cbind(Student, Age=c(20,21,22,23,24))
Print (Student_df)
12
21311A05L9 Kalyan. K
Output:
#create a new data frame include new row from the existing data frame.
Output:
# R commands for data frame functions cbind(), rbind(), sort() on Matrices # create
M1= 1 6 7
943
02 4
print(M1)
# Apply cbind() on M1
M2 = cbind(M1, c(1,2,3))
print(M2)
13
21311A05L9 Kalyan. K
Output:
M2 = 1 6 7 1
9432
0 2 43
rbind(M1, c(1,2,3))
print(M3) Output:
167
943
0 2 4
12 3
Output:
012344679
14
21311A05L9 Kalyan. K
[[1]]
[1] 10
[[3]]
[1] 1 2 3
[[4]]
[1] TRUE
my_list[[1]]
[1]
10
my_list[[3]] [1] 1 2 3
my_list[[4]]
[1] TRUE
length(my_list)
[1] 4
15
21311A05L9 Kalyan. K
ii. create a R Command for to create a list containing a vector, a matrix, and a list. Also give
names to the elements in the list and display the list also access the list elements.
my_list
#Output:
$my_vector [1]
123
$my_matrix
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
$my_nested_list
$my_nested_list
[[1]]
[1] "a"
$my_nested_list
[[2]]
[1] 2
$my_nested_list
[[3]]
[1] 1 2 3
16
21311A05L9 Kalyan. K
iii. To add a new element at the end of the list and delete the element from the middle
display the same
#Create a list
#Output:
[[1]]
[1] "kishore"
[[2]]
[1] "ram"
[[3]]
[1]"laxman"
[[4]]
[1] "balaji"
my_list[[length(my_list)+1]]='sunny' my_list
#Output:
[[1]]
[1] "kishore"
[[2]]
[1] "ram"
[[3]]
[1] "laxman"
[[4]]
[1] "balaji"
[[5]]
[1] "sunny"
my_list=my_list[-3]
my_list
17
21311A05L9 Kalyan. K
#Output
[[1]]
[1] "kishore"
[[2]]
[1] "ram"
[[3]]
[1] "balaji"
[[4]]
[1] "sunny"
iv. To create two lists, merge two lists. Convert the lists into vectors and perform addition on
the two vectors. Display the resultant vector.
18
21311A05L9 Kalyan. K
Example 2 :
l1=list(1,2,3)
l2=list(4,5,6)
ml=c(l1,l2)
v1=unlist(l1)
v2=unlist(l2)
rv=v1+v2
Rv
Output: [1] 5 7 9
19
21311A05L9 Kalyan. K
# Example 2:
vector1 <- c(TRUE, FALSE, TRUE)
vector2 <- c(TRUE, TRUE, FALSE)
result <- vector1 & vector2
print(result)
# Output: TRUE FALSE FALSE
OR Operator (|):
# Example 1:
x <- TRUE
y <- FALSE
result <- x | y
print(result)
# Output: TRUE
# Example 2:
vector1 <- c(TRUE, FALSE, TRUE)
vector2 <- c(TRUE, TRUE, FALSE)
result <- vector1 | vector2
print(result)
# Output: TRUE TRUE TRUE
20
21311A05L9 Kalyan. K
# Example 2:
vector <- c(TRUE, FALSE, TRUE)
result <- !vector
print(result)
# Output: FALSE TRUE FALSE
Conditional Statements
ii) Create four vectors namely patientid, age, diabetes, and status. Put these four
vectors into a data frame patient data and print the values using a for loop &
While loop
21
21311A05L9 Kalyan. K
Output:
22
21311A05L9 Kalyan. K
Output:
23
21311A05L9 Kalyan. K
iv. Recursion function for a) factorial of a number b) find nth Fibonacci number
Output:
24
21311A05L9 Kalyan. K
In this program, we define a function called quick_sort that takes an array arr as
an
argument and returns the sorted array. The function first checks if the length of the array is
less than or equal to 1, and if so, returns the array as is. Otherwise, it selects a pivot element
(in this case, the first element of the array), and partitions the array into two subarrays: one
containing elements less than or equal to the pivot, and another containing elements greater
than the pivot. It then recursively applies the quick sort algorithm to each subarray and
concatenates the sorted subarrays with the pivot element.
We then test the function with an example array arr and print the original and sorted
arrays using the cat() function.
Here's the output of the program:
25
21311A05L9 Kalyan. K
26
21311A05L9 Kalyan. K
27
21311A05L9 Kalyan. K
28
21311A05L9 Kalyan. K
9. Write R command to
i) illustrate Mathematical functions & I/O functions
i) Mathematical functions:
# Example of mathematical functions in R
# Square root function
sqrt(16)
# Exponential function
exp(2)
# Natural logarithm function
log(10)
# Sine function (in radians)
sin(pi/2)
Output:
[1] 4
[1] 7.389056
[1] 2.302585
[1] 1
I/O functions:
# Example of I/O functions in R
# Read input from the user
name <- readline(prompt = "Enter your name: ")
cat("Hello, ", name, "!")
# Write output to a file
data <- c(1, 2, 3, 4, 5)
write(data, file = "output.txt")
# Read input from a file
input_data <- scan(file = "input.txt")
cat("The input data is: ", input_data, "\n")
Output:
Enter your name: John
Hello, John!
The input data is: 1 2 3 4 5
29
21311A05L9 Kalyan. K
In this example, we use the readline() function to read input from the user and the cat()
function to print output to the console. We also use the write() function to write output to a
file and the scan() function to read input from a file. Note that in this example, we assume
that there are two files named input.txt and output.txt in the current working directory.
ii. Illustrate Naming of functions and sapply(), lapply(), tapply() & mapply()
30
21311A05L9 Kalyan. K
Output:
[1] 3 4 5 6
$a
[1] 3
$b
[1] 4
$c
[1] 5
$A
[1] 3 5
$B
[1] 4 6
[1] 3 5 5 7
In this example, we define a named function add_two() that adds 2 to its input. We
then use the sapply() function to apply the function to a vector, the lapply() function to apply
the function to a list, the tapply() function to apply the function to subsets of a vector based
on a grouping variable, and the mapply() function to apply the function to multiple vectors.
Note that the sapply() function simplifies the output to a vector, while the lapply() function
preserves the output as a list. The tapply() function applies the function to subsets of the
input vector based on a grouping variable, and the mapply() function applies the function to
multiple vectors element- wise.
31
21311A05L9 Kalyan. K
i) Pie chart & 3D Pie Chart, Bar Chart to demonstrate the percentage conveyance
of various ways for traveling to office such as walking, car, bus, cycle, and train
In this example, we first create a data frame with two columns: conveyance and
percentage, which store the conveyance modes and their corresponding percentages. We
then create a pie chart using the pie() function, a 3D pie chart using the pie3D() function
from the plotrix package, and a bar chart using the barplot() function. All three charts
display the same data, but provide different visual representations of the conveyance
percentages.
ii) Using a chart legend, show the percentage conveyance of various ways for
traveling to office such as walking, car, bus, cycle, and train.
(a) Walking is assigned red color, car – blue color, bus – yellow color, cycle –
green color, and train – white color; all these values are assigned through
cols and lbls variables and the legend function.
(b) The fill parameter is used to assign colors to the legend.
(c) Legend is added to the top-right side of the chart, by assigning
32
21311A05L9 Kalyan. K
In this example, we first create a data frame with two columns: conveyance and
percentage, which store the conveyance modes and their corresponding percentages. We
then create a bar chart using the barplot() function and assign colors to each conveyance
mode using a vector cols. We assign the conveyance modes to the colors using the names()
function.
Next, we create a legend for the chart using the legend() function. We specify the
location of the legend using the "topright" argument, and provide the legend labels and
colors using the legend and fill arguments, respectively. The colors in the legend match
the colors used in the bar chart.
Overall, this program creates a bar chart with a legend that shows the percentage
conveyance of various ways for traveling to office, and assigns different colors to each
conveyance mode. Using box plots, Histogram, Line Graph, Multiple line graphs and
scatter plot to demonstrate the relation between the cars speed and the distance taken to stop,
Consider the parameters data and x Display the speed and dist parameter of Cars data set
using x and data parameters
iii. Using box plots, Histogram, Line Graph, Multiple line graphs and scatter plot to
demonstrate the relation between the cars speed and the distance taken to stop,
Consider the parameters data and x Display the speed and dist parameter of
Cars data set using x and data parameters
33
21311A05L9 Kalyan. K
# Display the speed and dist parameters using x and data parameters
x <- cars$speed
data <- cars$dist
In this program, we first load the cars dataset using the data() function. We then
assign the speed and dist parameters to variables x and data, respectively.
We then use various plotting functions to demonstrate the relationship between the
car's speed and the distance taken to stop. The boxplot() function is used to create a box plot
of the data, while the hist() function is used to create a histogram.
We also use the plot() function to create a line graph of the data, and the par()
function to create multiple line graphs in a 2x2 grid. Finally, we use the plot() function to
create a scatter plot of the data. Overall, this program provides a range of visualization options
for exploring the relationship between the car's speed and the distance taken to stop.
34