Amit kumar 22391020 MCA 1ST 13(C)
Problem statement 16. Program to check if the input year is a leap year or
not.
Script:
year = as.integer(readline(prompt="Enter a year:
")) if((year %% 4) == 0) { if((year %% 100) ==
0) { if((year %% 400) == 0)
{ print(paste(year,"is a leap year"))
} else { print(paste(year,"is not
a leap year"))
} else { print(paste(year,"is a
leap year"))
} else { print(paste(year,"is not a
leap year"))
OUTPUT:
Amit kumar 22391020 MCA 1ST 13(C)
Problem statement 17 Create, access, modify and delete following
data structures in R
a) Vectors
b) Lists
c) Data Frame
d) Factor
e) Matrix
a) Create a Vector in R: vec <- c(1,2,3,4,5)
Access Vector in R:
vec[1]
Modify Vector in R:
vec[1] <- 6
Delete Vector in R:
rm(vec)
b) Create a List in R:
list <- list(a = 1, b = 2, c = 3, d = 4, e = 5)
Access List in R:
list$a
Amit kumar 22391020 MCA 1ST 13(C)
Modify List in R:
list$a <- 6
Delete List in R:
rm(list)
c) Create a Data Frame in R:
df <- data.frame(a = c(1,2,3), b = c(4,5,6))
Access Data Frame in R:
df$a
Modify Data Frame in R:
df$a[1] <- 7
Delete Data Frame in R:
rm(df)
d) Create a Factor in R:
fac <- factor(c("Low", "Medium", "High"), levels = c("Low", "Medium",
"High"))
Access Factor in R:
fac[1]
Modify Factor in R:
Amit kumar 22391020 MCA 1ST 13(C)
fac[1] <- "Medium"
Delete Factor in R:
rm(fac)
e) Create a Matrix in R:
mat <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3)
Access Matrix in R:
mat[1,1]
Modify Matrix in R:
mat[1,1] <- 7
Delete Matrix in R:
rm(mat)
OUTPUT:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
NAME – ROHIT KUMAR PANDEY STUDENT ID: 22391252
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
Problem statement 18 : Create a function to print squares of numbers in sequence in r
language.
Script
square_num <- function(n)
{ for (i in
1:n)
print(i^2)
square_num(5)
OUTPUT:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
Problem Statement 19 : Demonstrate various Numerical, Character and Statistical functions used
in R.
Numerical functions:
• mean() - Calculates the arithmetic mean of a numeric vector.
• max() - Returns the maximum value of a numeric vector.
• min() - Returns the minimum value of a numeric vector.
• sqrt() - Returns the square root of a number.
• sum() - Calculates the sum of values in a numeric vector.
• round() - Rounds a numeric vector to the specified number of digits.
Character functions:
• toupper() - Converts a character vector to all uppercase.
• tolower() - Converts a character vector to all lowercase.
• nchar() - Returns the number of characters in a character vector.
• substr() - Extracts substrings from a character vector.
• paste() - Concatenates strings in a character vector.
Statistical functions:
• sd() - Calculates the standard deviation of a numeric vector.
• var() - Calculates the variance of a numeric vector.
• quantile() - Calculates the quantiles of a numeric vector.
• cor() - Calculates the correlation coefficient between two numeric vectors.
• summary() - Returns a summary of a numeric vector.
CODE:
# R program to calculate
# Arithmetic mean of a vector
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
# Creating a numeric vector
x1 <- c(1, 2, 3, 4, 5, 6)
x2 <-c(1.2, 2.3, 3.4, 4.5)
x3 <- c(-1, -2, -3, -4, -5,6)
# Calling mean() function
mean(x1) mean(x2)
mean(x3)
OUTPUT:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
#creates a vector returns the max values present in the vector
vector<-c(45.6,78.8,65.0,78.9,456.7,345.89,87.6,988.3)
max(vector)
OUTPUT:
988.3
x1 <- c(4, 1, - 50, 20, 8) # Create example vector
min(x1) # Apply min to vector #
-50
# square root:-
x1 <- c(1, 2, 3, 4, 5, 6) sqrt(x1)
OUTPUT:
sum & round:-
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
Character Function:
# R program to illustrate to uppercase of vectors
Create example character string
x <- "GeeksforGeeks"
toupper(x)
print(x)
OUTPUT:
# R program to illustrate to lowercase of vectors
Create example character string
x <- "GeeksforGeeks"
tolower(x)
print(x)
OUTPUT:
# R program to calculate lengthof string Using nchar() method
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
Given String
gfg < - "Geeks For Geeks"
answer < - nchar(gfg)
print(answer)
OUTPUT:
CREATING PASTE() FUNCTION:
paste(1,'two',3,'four',5,'six')
Output = “1 two 3 four 5 six”
Statistical functions:
• sd() - Calculates the standard deviation of a numeric vector.
x1 <- c(1, 2, 3, 4, 5, 6)
sd(x1)
OUTPUT:
• var() - Calculates the variance of a numeric vector.
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
x1 <- c(1, 2, 3, 4, 5, 6)
var(x1)
Output:
• quantile() - Calculates the quantiles of a numeric vector.
x1 <- c(1, 2, 3, 4, 5, 6)
quantile(x1)
Output:
• cor() - Calculates the correlation coefficient between two numeric vectors.
x1 <- c(1, 2, 3, 4, 5, 6)
x2<-c(2,5,6,4,6,9)
cor(x1,x2)
Output:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
• summary() - Returns a summary of a numeric vector.
x1 <- c(1, 2, 3, 4, 5, 6)
summary(x1)
Output:
Problem statement 20: The numbers below are the first ten days of rainfall amounts in
1996.Read them in to a vector using the c() function 0.1, 0.6, 33.8, 1.9, 9.6, 4.3, 33.7, 0.3,
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
0.0, 0.1
a. What was the mean rainfall, how about the standard deviation?
b.Calculate the cumulative rainfall (’running total’) over these ten days. Confirm that the last
value of the vector that this produces is equal to the total sum of the rainfall.
cWhich day saw the highest rainfall?
Code:
Calculate of mean:
x1 <- c(0.1, 0.6, 33.8, 1.9, 9.6, 4.3, 33.7, 0.3, 0.0, 0.1)
mean(x1) Output:
Standard Deviation:
x1 <- c(0.1, 0.6, 33.8, 1.9, 9.6, 4.3, 33.7, 0.3, 0.0, 0.1) sd(x1)
Output:
B Cumulative rainfall = c(0.1, 0.7, 34.5, 36.4, 45.9, 50.2, 83.9,
84.2, 84.2, 84.3) x1 <- c(0.1, 0.6, 33.8, 1.9, 9.6, 4.3, 33.7, 0.3, 0.0,
0.1) sum(x1) Output:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
b. Day 3 saw the highest rainfall with x1 <- c(0.1, 0.6, 33.8,
1.9, 9.6, 4.3, 33.7, 0.3, 0.0, 0.1) max(x1)
Output:
Problem statement 21 Demonstrate the various function used for Graphical Analysis like
creating box plot, scatters plot, line graph and pie charts and bar chart in R language.
Box Plot
Create data
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
x = c(1,2,3,4,5,6,7,8,9,10) # Create the
boxplot boxplot(x, main="Boxplot",
ylab="Values") OUTPUT:
# Scatter Plot
Create data
x = c(1,2,3,4,5,6,7,8,9,10)
y=c(1,4,9,16,25,36,49,64,81,100)
# Create the scatterplot
plot(x, y,
main="Scatterplot",
xlab="X Values",
ylab="Y Values")
Output:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
# Line Graph # Create data x =
c(1,2,3,4,5,6,7,8,9,10) y =
c(1,4,9,16,25,36,49,64,81,100)
# Create the line graph plot(x, y, type="l", main="Line Graph", xlab="X
Values", ylab="Y Values")
Output:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
# Pie Chart # Create
data x = c(25,15,35,25)
# Create the pie chart
pie(x, main="Pie
Chart") Output:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
# Bar Chart # Create data x = c(1,2,3,4,5,6,7,8,9,10) # Create
the bar chart barplot(x, main="Bar Chart", xlab="X Values",
ylab="Y Values") Output:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
Problem statement22. Demonstrate Implementation of ANOVA in R Studio .
# creating a sample dataset
set.seed(123)
# creating a data frame
Exp_data <- data.frame(ID = c(1:20),
Treatment = sample(c("A", "B", "C"), 20, replace = TRUE),
Score = sample(10:20, 20, replace = TRUE))
# viewing the data frame
Exp_data
Output:
Deepanshu jaiswal 22391151 MCA 1ST 26(C)
# performing ANOVA test anova_test <- aov(Score ~
Treatment, data = Exp_data)
# viewing the summary of ANOVA
summary(anova_test)
Output: