R Programmimg Practical Journal All-1
R Programmimg Practical Journal All-1
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-
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-
Output-
3]Implement lazy evaluation of functions
Source Code-
fun1<-function(a,b){
print(a)
}
fun1(10,20)
Output-
#Repeat Statement
Source Code-
i<-1
repeat{
print(i)
i<-i+1
if(i>6)
break
}
Output-
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-
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-
Output-