r Lab Manual
r Lab Manual
1. Write a program to check whether a year(integer)entered by the user is a leap year or not?
Program:
y = as.numeric(readline(prompt = 'year'))
if ((y %% 4) == 0){
if ((y %% 100) == 0){
if ((y%% 400) ==0){
print(paste(y,'is a leap year'))
}
else {
print(paste(y,"is not a leap year"))
}
}
else{
print(paste(y,"is a leap year"))
}
}else{
print(paste(y,"is not a leap year"))
}
Output:
2. Write an R program to find the sum of natural without formula using the if-else statement and
while loop.
Program:
Output:
Enter a number: 10
Program:
print (‘A’)
elif ( percentage >80 and percentage <= 90 ):
print (‘B’)
Output
[1] "Select operation."
[1] "1.Add"
[1] "2.Subtract"
[1] "3.Multiply"
[1] "4.Divide"
Enter choice[1/2/3/4]: 4
Enter first number: 20
Enter second number: 4
[1] "20 / 4 = 5"
5. Write a program to perform searching within a list (1 to 50). If the number is found in the list, print
that the search is successful otherwise print that the number is not in the list.
Program:
if (i %in% list1) {
print("Item is present in the List.")
} else {
print("Item is not present in the List.")
}
Output:
>list1 <- seq(from=1, to=50, by= 1.5)
> print(list1)
[1] 1.0 2.5 4.0 5.5 7.0 8.5 10.0 11.5 13.0 14.5 16.0 17.5 19.0 20.5 22.0 23.5 25.0 26.5
[19] 28.0 29.5 31.0 32.5 34.0 35.5 37.0 38.5 40.0 41.5 43.0 44.5 46.0 47.5 49.0
> i= readline()
43.5
> if (i %in% list1) {
+ print("Item is present in the List.")
+ } else {
+ print("Item is not present in the List.")
+ }
[1] "Item is not present in the List."
6. Create a list and data frame that stores the marks of any three subjects for 10 students. Find out the
total marks, average, maximum marks and minimum marks of every subject.
Program :-
maths <- c(65, 78, 89, 54, 79, 75, 88, 67, 92, 85)
physics <- c(51, 67, 54, 52, 81, 72, 85, 83, 62, 66)
chemistry <- c(34, 65, 54, 55, 65, 75, 89, 57, 87, 63)
[[2]]
[1] 51 67 54 52 81 72 85 83 62 66
[[3]]
[1] 34 65 54 55 65 75 89 57 87 63
7. Write the steps to import data from Excel to CSV files and apply data viewer functions like
rm(),dim(),head(), tail(), sorting, filtering, searching to view few set of rows.
Steps:
Transform an Excel file to a CSV file
8. Write a program to create two 3 X 3 matrices A and B and perform the following operations:
a) Transpose of the matrix.
b) addition.
c) subtraction.
Program:
data <- c(1, 2, 7, 2, 8, 4, 3, 0, 9)
data1 <- c(5, 2, 7, 6, 1, 4, 2, 7, 5)
A <- matrix(data, nrow = 3, ncol = 3)
B <- matrix(data, nrow = 3, ncol = 3)
A_T <- t(A)
print("Matrix A")
print(A)
print(“Matrix B”)
print(B)
print("Transpose of A")
print(A_T)
#Addition of matrix
print(“Addition of two matrices”)
print(A+B)
print(“Subtraction of two matrices”)
print(A-B)
Output
[1] "Matrix A"
> print(A)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 8 0
[3,] 7 4 9
> print("Print B")
[1] "Print B"
> print(B)
[,1] [,2] [,3]
[1,] 5 6 2
[2,] 2 1 7
[3,] 7 4 5
> print("Transpose of A")
[1] "Transpose of A"
> print(A_T)
[,1] [,2] [,3]
[1,] 1 2 7
[2,] 2 8 4
[3,] 3 0 9
> #Addition of matrix
> print("Addition of matrix")
[1] "Addition of matrix"
> print(A+B)
[,1] [,2] [,3]
[1,] 6 8 5
[2,] 4 9 7
[3,] 14 8 14
9. Write an R program to create a list containing strings, numbers, vectors and logical values and do
the
following manipulations over the list.
a. Access the first element in the list
b. Give the names to the elements in the list
c. Add element at some position in the list
d. Remove the element
e. Print the fourth element
f. Update the third element
Program:
list_data = list("Python", "PHP", c(5, 7, 9, 11), TRUE, 125.17, 75.83)
print("Data of the list:")
print(list_data)
#Access first element in the list
list_data[[1]][1]
#Give the names to the element
names(list_data) <- c("Programming Language", "Pages", "A Vector", "Boolean", "Double", "Double"
)
print(list_data)
##Add element at some position in the list
list_data <- append(list_data, "Tuesday", after = 2)
print(list_data)
#Remove the element
list_data[c(1, 3)] = NULL
#Print the fourth element
print(list_data[4])
#Update the third element
list_data[3] <- "updated element"
print(list_data[3])
Output:
[1] "Data of the list:"
> print(list_data)
[[1]]
[1] "Python"
[[2]]
[1] "PHP"
[[3]]
[1] 5 7 9 11
[[4]]
[1] TRUE
[[5]]
[1] 125.17
[[6]]
[1] 75.83
> list_data[[1]][1]
[1] "Python"
> names(list_data) <- c("Programming Language", "Pages", "A Vector", "Boolean", "Double", "Doubl
e")
> print(list_data)
$`Programming Language`
[1] "Python"
$Pages
[1] "PHP"
$`A Vector`
[1] 5 7 9 11
$Boolean
[1] TRUE
$Double
[1] 125.17
$Double
[1] 75.83
$Pages
[1] "PHP"
$`A Vector`
[1] 5 7 9 11
$Boolean
[1] TRUE
$Double
[1] 125.17
$Double
[1] 75.83
[[7]]
[1] "Tuesday"
$Boolean
[1] TRUE
$Double
[1] 125.17
$Double
[1] 75.83
[[5]]
[1] "Tuesday"
$Boolean
[1] TRUE
$Double
[1] 125.17
[[4]]
[1] "Tuesday"
> print(list_data[4])
[[1]]
[1] "Tuesday"
10. Design a data frame in R for storing about 20 employee details. Create a CSV file named
“input.csv” that defines all the required information about the employee such as id, name, salary,
start_date, dept. Import into R and do the following analysis.
a. Find the total number rows & columns
b. Find the maximum salary
c. Retrieve the details of the employee with maximum salary
d. Retrieve all the employees working in the IT Department
e. Retrieve the employees in the IT Department whose salary is greater than 20000 and write these
details into another file “output.csv”.
11. Create a dataset or table [‘Smart Phone”] in an excel sheet that stores the mobile information
[price, company name, model, Sale Percent] of five different companies. Store at least 20 rows. Write
the scripts and find out the output for the following information.
a. Maximum price of the mobile of each company
b. Minimum price of mobile of each company
c. Average price of mobile of each company
d. Total Price of mobile of each company
Program: