R Programming LAB - IV SEM - BCA
R Programming LAB - IV SEM - BCA
1. Create a vector and apply different operations on the vector using r programming
Aim : To create a vector and apply different operations on the vector using r programming
Description :
Vectors in r can have a fixed length, and elements can be accessed using indexing.
Operations and functions can be applied to vectors, such as arithmetic calculations, subsetting,
aggregation functions (e.g., sum(), mean()), and various other vectorized operations.
Source code :
# creating a vector
Print(sqrt_vector)
Print(log_vector)
Print(exp_vector)
Print(abs_vector)
Output :
[1] 2 4 6 8 10
2. Create a matrix and apply different operations on the matrix using r programming
Aim : To create a matrix and apply different operations on matrix using r programming
Description :
Matrices in r can be manipulated and operated upon using various functions and
operations. You can perform operations like matrix addition, subtraction, multiplication,
transpose, matrix multiplication, and more.
Source code :
# creating a matrix
Print(my_matrix)
Print(transpose_matrix)
# addition of matrices
Print(addition_matrix)
# subtraction of matrices
Print(subtraction_matrix)
P.V.V.SANDEEP MCA 2
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
Print(multiplication_matrix)
# matrix multiplication
Print(matrix_product)
Output :
[1,] 1 2 3
[2,] 4 5 6
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
[1,] 2 4 6
[2,] 8 10 12
[1,] 0 0 0
[2,] 0 0 0
[1,] 1 4 9
[2,] 16 25 36
[,1] [,2]
[1,] 14 32
P.V.V.SANDEEP MCA 3
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
[2,] 32 77
Aim : To create a arrays and apply different operations on arrays using r programming
Description :
Arrays are multi-dimensional data structures that can hold elements of the same data
type. Arrays can have more than two dimensions, whereas matrices are limited to two
dimensions. To create an array in r, you can use the array() function, specifying the data,
dimensions, and optionally, dimension names.
Source code:
# creating an array
Print(my_array)
Print(element)
Print(sum_array)
Print(mean_array)
Output:
,,1
[1,] 1 3 5
[2,] 2 4 6
P.V.V.SANDEEP MCA 4
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
,,2
[1,] 1 3 5
[2,] 2 4 6
[1] 3
[1] 36
[1] 3
4. Create a simple list and apply different operations on list using r programming
Aim : To create a simple list and apply different operations on list using r programming
Description :
A list is a versatile data structure that can hold elements of different data types. Lists are
created using the list() function and can contain vectors, matrices, data frames, and even
other lists as elements.
Source code:
# creating a list
Age = 30,
Is_employed = true
Print(my_list$name)
Print(my_list$age)
Print(my_list$scores)
Print(my_list$is_employed)
P.V.V.SANDEEP MCA 5
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
Output :
[1] 30
[1] 90 85 95
[1] true
5. Create a simple data frame and apply different operations on data frame using r
programming
Aim : To create a simple data frame and apply different operations on data frame using r
programming
Description :
A data frame is a two-dimensional tabular data structure that stores data in rows and
columns. Dataframes are commonly used for data manipulation, analysis, and modeling
Source code :
Stringsasfactors = false
Print(my_df)
Print(summary_df)
Output :
P.V.V.SANDEEP MCA 6
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
1 john 25 90
2 jane 30 85
3 mark 28 95
4 emily 27 88
6. Create a simple data frame and apply packages on data frame using r programming
Aim : To create a simple data frame and apply packages on data frame using r programming
Description :
To apply packages on a data frame in r, you need to first install the required packages
using the install.packages() function. Once installed, you can load the packages using the
library() function.
The dplyr package is installed using install.packages() and loaded using library(). A
simple data frame called my_df is created. Different functions from the dplyr package, such as
filter(), mutate(), summarize(), arrange(), and select(), are then applied to the data frame to
perform filtering, mutation, summarization, sorting, and column selection, respectively.
Source code :
Install.packages("Dplyr")
Library(dplyr)
Stringsasfactors = false
Print(filtered_df)
Print(mutated_df)
Print(summarized_df)
Output :
1 jane 30 85
2 mark 28 95
3 emily 27 88
1 john 25 90 180
2 jane 30 85 170
3 mark 28 95 190
4 emily 27 88 176
Avg_score
1 89.5
P.V.V.SANDEEP MCA 8
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
7. Create a simple data frame and apply some basic statistics on the data frame using r
programming
Aim : To create a simple data frame and apply some basic statistics on the data frame using r
programming
Description:
Different basic statistical functions such as summary(), mean(), median(), min(), max(),
var(), sd(), and range() are applied to the data frame to calculate summary statistics, mean,
median, minimum value, maximum value, variance, standard deviation, and range of the
score column.
Source code :
Stringsasfactors = false
Print(my_df)
Print(summary_stats)
Print(mean_score)
Print(median_score)
Print(min_score)
Print(max_score)
Print(var_score)
Print(sd_score)
Print(range_score)
Output :
1 john 25 90
2 jane 30 85
3 mark 28 95
4 emily 27 88
[1] 89.5
P.V.V.SANDEEP MCA 10
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
[1] 89
[1] 85
[1] 95
[1] 27.66667
[1] 5.259911
[1] 85 95
8. Create a simple data frame and apply basic analysis techniques on data frame using r
programming
Aim : To create a simple data frame and apply basic analysis techniques on data frame using r
programming
Description:
Various analysis techniques are then applied to the data frame, including analyzing the
structure of the data frame using str(), calculating summary statistics using summary(),
calculating the correlation between variables using cor(), performing a linear regression using
lm(), and making predictions using the linear regression model with predict().
Source code :
Stringsasfactors = false
Print(my_df)
Str(my_df)
P.V.V.SANDEEP MCA 11
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
Summary(my_df)
Print(correlation)
Print(linear_model)
Print(predicted_scores)
Output :
1 john 25 90
2 jane 30 85
3 mark 28 95
4 emily 27 88
$ age : Num 25 30 28 27
$ score: Num 90 85 95 88
P.V.V.SANDEEP MCA 12
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
[1] -0.1951801
Call:
Coefficients:
(intercept) age
94.7647 -0.5294
1 2
92.82353 90.70588
9. Create a simple data frame and apply data analysis techniques on data frame using r
programming
Aim : To create a simple data frame and apply data analysis techniques on data frame using r
programming
Description :
Linear regression is applied using the lm() function to model the relationship between
score, age, and income variables. The output shows the regression coefficients. Then,
clustering techniques (specifically k-means clustering) are applied using the kmeans() function.
The selected variables for clustering are scaled using the scale() function. The result shows the
cluster means, clustering vector, within-cluster sum of squares, and available components.
Output :
P.V.V.SANDEEP MCA 13
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
Stringsasfactors = false
Print(my_df)
Print(linear_model)
Set.seed(123)
Print(kmeans_result)
Output :
1 john 25 90 50000
2 jane 30 85 60000
3 mark 28 95 70000
4 emily 27 88 55000
5 adam 35 75 80000
6 sarah 32 80 75000
P.V.V.SANDEEP MCA 14
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
Call:
Coefficients:
Cluster means:
Clustering vector:
1 1 1 1 3 3
Available components:
10. Create a simple data frame and apply data visualization techniques heatmap on data
frame using r programming
Aim : To create a simple data frame and apply data visualization techniques heatmap on data
frame using r programming
Description :
Heatmap is a type of data visualization that represents data in a tabular format using colors to
P.V.V.SANDEEP MCA 15
PRAGATI WOMENS DEGREE COLLEGE R Programming II BCA – IV SEM
indicate the relative values of the data points. It is a useful tool for exploring and analyzing
complex data sets, particularly those with large numbers of variables or observations.
Source code :
Output :
P.V.V.SANDEEP MCA 16