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

R Programmimg Practical Journal All-1

The document outlines an R Programming course covering topics such as basics of R, user-defined functions, matrices and arrays, data frames, and data visualization. It provides example code and output for assignments on these topics, including functions, matrices operations, data wrangling, and creating visualizations from data. The assignments aim to teach essential R programming concepts and skills through hands-on practice of coding exercises and examples.

Uploaded by

Hãrîsh Pàwár
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)
23 views

R Programmimg Practical Journal All-1

The document outlines an R Programming course covering topics such as basics of R, user-defined functions, matrices and arrays, data frames, and data visualization. It provides example code and output for assignments on these topics, including functions, matrices operations, data wrangling, and creating visualizations from data. The assignments aim to teach essential R programming concepts and skills through hands-on practice of coding exercises and examples.

Uploaded by

Hãrîsh Pàwár
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/ 25

Vasantraodada Patil Institute of Management Studies & Research, Sangli

BCA-III Sem-VI Batch 2023-24


R Programming
INDEX
Sr.
Assignment Date Signature
No.

Basics of R Programming:
1] Accept any number from user and display its factorial.
2] Accept weight, height and gender from user and
display his/her BMI with a proper message.
3] Accept a digit and display its spelling (e.g. Input: 8
1 Output: Eight). 5/1/2024
4] Display following pattern for given number of lines
*
* *
* * *
* * * *

User-defined function:
1] Find sum of 1!+2!+3!+4!+5! By using factorial
function
2] Implement local and global variables
2 20/1/2024
3] Implement lazy evaluation of functions
4] Implement repeat, next and break statement
5] Find BMI according to user’s weight, height and
gender.
Matrices, Arrays and Dataframes:
1] Construct a 2x4 matrix with predetermined values of
your choice.
2] Access the element at the 2nd row and 3rd column of
a given matrix.
3 15/2/2024
3] Extract the entire second row from a matrix
4] Perform element-wise addition of two matrices of the
same dimensions.
5] Transpose a square matrix.
6] Generate a 2-dimensional array of shape (3, 4)
initialized with random numbers
7] Extract the entire second column from a 2-
dimensional array.
8] Find the maximum value in a 2-dimensional array.
9] Create a data frame with three columns: "Name",
"Age", and "Gender", and populate it with sample data.
10] Display the first 5 rows of a data frame.
11] Retrieve summary statistics (mean, median, min, max,
etc.) for numerical columns in a data frame.
12] Filter rows based on a certain condition (e.g., age
greater than 30).

Data Visualization:
1] Import a dataset (e.g., CSV file) into your
programming environment.
2] Identify and count the number of missing values in
each column of the dataset.
3] Handle missing data by either removing or imputing
4 missing values. 28/2/2024
4] Create a scatter plot to visualize the relationship
between two numerical variables from the dataset.
5] Create a bar plot to display the frequency or count of
categorical variables.
6] Create a pie chart to represent the distribution of a
categorical variable.
1. Basics of R Programming:
1] Accept any number from user and display its factorial.
Source Code-
n<-as.numeric(readline("Enter the number:"))
fact<-1
for(i in 2:n){
fact<-fact*i
}
print(paste(n,"!=",fact))
Output-

2] Accept weight, height and gender from user and display


his/her BMI with a proper message.
Source Code-
height<-as.numeric(readline("Enter the Height in m:"))
weight<-as.numeric(readline("Enter the Weight in kg"))
gender<-as.character(readline("Enter the Gender:"))
if(gender=="Male"){
print("male")
}else if(gender=="female"){
print("Female")
}
bmi<-weight/(height^2)
if(bmi<18.5){
print("Underweight")
}else if(bmi<25){
print("Normal Weight")
}else if(bmi<30){
print("Overweight")
}else{
print("Invalid Input")
}
Output-
3] Accept a digit and display its spelling (e.g. Input: 8
Output: Eight).
Source Code-
n=as.numeric(readline("Enter any digit from 1-9"))
if(n==0){
print("zero")
}else if(n==1){
print("One")
}else if(n==2){
print("Two")
}else if(n==3){
print("Three")
}else if(n==4){
print("Four")
}else if(n==5){
print("Five")
}else if(n==6){
print("Six")
}else if(n==7){
print("Seven")
}else if(n==8){
print("Eight")
}else if(n==9){
print("Nine")
}else{
print("Invalid Input")
}

Output-

4] Display following pattern for given number of lines


*
* *
* * *
* * * *
Source Code-
for(i in 1:4){
line<-"\n"
for(j in 1:i){
line<-paste(line,"*")
}
cat("\n",line)
}
Output-

2. User-defined function:
1]Find sum of 1!+2!+3!+4!+5! By using factorial function
Source Code-
fact <- function(i) {
if (i == 0 || i == 1) {
return(1)
} else {
return(i * fact(i - 1))
}
}
sum <- 0
for (i in 1:5) {
sum <- sum + fact(i)
}
print(paste("Sum of factorials from 1 to 5 is:", sum))
Output-

2]Implement local and global variables.


Source Code-
a<-20
myfunction<-function(){
b<-30
print(paste(b,"b is the local variable "))
}
myfunction()
print(paste(a,"a is the global variable"))

Output-
3]Implement lazy evaluation of functions
Source Code-
fun1<-function(a,b){
print(a)
}
fun1(10,20)
Output-

4]Implement repeat, next and break statement


Source Code-
#Break and Next Statement
for(i in 1:9){
if(i==6){
next
}
else if(i==4){
break
}
print(i)
}
Output-

#Repeat Statement
Source Code-
i<-1
repeat{
print(i)
i<-i+1
if(i>6)
break
}
Output-

4]Find BMI according to user’s weight, height and gender.


Source Code-
bmi<-function(height,weight,gender){
if(gender=="Male"){
print("male")
}else if(gender=="female"){
print("Female")
}
bmi<-weight/(height^2)
if(bmi<18.5){
print("Underweight")
}else if(bmi<25){
print("Normal Weight")
}else if(bmi<30){
print("Overweight")
}else{
print("Invalid Input")
}
}
bmi(135,49,"Female")
Output-
3.Matrices, Arrays and Dataframes:
1]Construct a 2x4 matrix with predetermined values of your
choice.
Source Code-
a<-c(2,3,6,7)
b<-c(1,4,5,8)
c<-rbind(a,b)
c
Output-

2]Access the element at the 2nd row and 3rd column of a


given matrix.
Source Code-
a<-c(2,3,6,7)
b<-c(1,4,5,8)
d<-rbind(a,b)
d
print(d[2,3])
Output-

3]Extract the entire second row from a matrix


Source Code-
a<-c(2,3,6,7)
b<-c(1,4,5,8)
d<-rbind(a,b)
d
print(d[2,])
Output-
4]Perform element-wise addition of two matrices of the same
dimensions.
Source Code-
mat1<-matrix(1:6,nrow = 2)
mat2<-matrix(7:12,nrow=2)
result<-mat1+mat2
result
Output-

5]Transpose a square matrix.


Source Code-
mat1<-matrix(1:6,nrow = 2)
a<-t(mat1)
a
Output-
6]Generate a 2-dimensional array of shape (3, 4) initialized
with random numbers
Source Code-
random_array <- array(1:12, dim = c(3, 4))
print(random_array)
Output-

7] Extract the entire second column from a 2-dimensional


array.
Source Code-
random_array <- array(1:12, dim = c(3, 4))
random_array[ ,2]
Output-
8]Find the maximum value in a 2-dimensional array.
Source Code-
random_array <- array(1:12, dim = c(3, 4))
max<-max(random_array)
max
Output-

9] Create a data frame with three columns: "Name", "Age",


and "Gender", and populate it with sample data
Source Code-
stud<-data.frame(
Name=c("Tejas","Santosh","Shubham","Suhas"),
Age=c(20,22,21,24),
Gender=c("Male","Male","Male","Male")
)
Stud
Output-
10]Display the first 5 rows of a data frame.
Source Code-
stud<-data.frame(

Name=c("Shubham","Santosh","Vaishnavraj","Tejaswin
i","Suhas","Tejas"),
Age=c(20,22,21,24,19,20),

Gender=c("Male","Male","Male","Female","Male","Ma
le")
)
a<-head(stud,n=5)
a

Output-

11]Retrieve summary statistics (mean, median, min, max,


etc.) for numerical columns in a data frame.
Source Code-
stud<-data.frame(

Name=c("Saniya","Bhakti","Poorva","Rahul","Suchita",
"Sam"),
Age=c(20,22,21,24,19,20),

Gender=c("Female","Female","Female","Male","Femal
e","Male")
)
summary(stud)
mean_value<-mean(stud$Age)
mean_value
median_value<-median(stud$Age)
median_value
min_value<-min(stud$Age)
min_value
max_value<-max(stud$Age)
max_value

Output-
12]Filter rows based on a certain condition (e.g., age greater
than 30).
Source Code-
stud<-data.frame(
Name=c("Saniya","Bhakti","Poorva","Rahul","Suchita
","Sam"),
Age=c(20,22,21,24,40,20),
Gender=c("Female","Female","Female","Male","Fema
le","Male")
)
b <- stud[stud$Age > 30, ]
b

Output-

4.Data Visualization:
1]Import a dataset (e.g., CSV file) into your programming
environment.
Source Code-
install.packages("readxl")
library(readxl)
data<-(read_excel("E:\\studentdata.xlsx"))
data

Output-

2]Identify and count the number of missing values in each


column of the dataset.
Source Code-
is.na(data)
missing_values <- colSums(is.na(data))
missing_values
Output-

3] Handle missing data by either removing or imputing


missing values.
Source Code-
cc_mean<-mean(data$CC,na.rm = TRUE)
data$CC[is.na(data$CC)]<-cc_mean
data
mcom_mean<-mean(data$`M-com`,na.rm = TRUE)
data$`M-com`[is.na(data$`M-com`)]<-mcom_mean
data
r_mean<-mean(data$R,na.rm = TRUE)
data$R[is.na(data$R)]<-r_mean
data
Output-

4] Create a scatter plot to visualize the relationship between


two numerical variables from the dataset.
Source Code-
data <- data.frame(
X = c(1, 2, 3, 4, 5),
Y = c(2, 3, 5, 4, 6))
plot(data$X, data$Y,
main = "Scatter Plot of X vs Y",
xlab = "X",
ylab = "Y",
col = "blue",
pch = 16)
Output-

5]Create a bar plot to display the frequency or count of


categorical variables.
Source Code-
data <- data.frame(
Category = c("A", "B", "A", "C", "B", "A", "B", "C",
"C", "A")
)
category_counts <- table(data$Category)
barplot(category_counts,
main = "Frequency of Categorical Variables",
xlab = "Categories",
ylab = "Frequency",
col = "skyblue",
border = "black",
ylim = c(0, max(category_counts) + 1), # Adjust
ylim for better visualization
names.arg = names(category_counts))
Output-

6]Create a pie chart to represent the distribution of a


categorical variable.
Source Code-
data <- data.frame(
Category = c("A", "B", "A", "C", "B", "A", "B", "C",
"C", "A")
)

category_counts <- table(data$Category)


pie(category_counts,
main = "Distribution of Categorical Variable",
labels = paste(names(category_counts), ": ",
category_counts),
col = rainbow(length(category_counts)),
cex = 0.8,
clockwise = TRUE)

Output-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy