Practical Programs (1)
Practical Programs (1)
Sc(Computer Application)
1) Write a R program to take input from the user (name and age) and display the values.
Also print the version of R installation.
Output:
name = readline(prompt="Input your name: ")
Input your name: abcd
> age = readline(prompt="Input your age: ")
Input your age: 23
> print(paste("My name is",name, "and I am",age ,"years old."))
[1] "My name is abcd and I am 23 years old."
> print(R.version.string)
[1] "R version 4.3.1 (2023-06-16 ucrt)"
2) Write a R program to create a sequence of numbers from 20 to 50 and find the mean of
numbers from 20 to 60 and sum of numbers from 51 to 91.
Output:
[1] "Sequence of numbers from 20 to 50:"
[1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
[26] 45 46 47 48 49 50
[1] "Mean of numbers from 20 to 60:"
[1] 40
[1] "Sum of numbers from 51 to 91:"
[1] 2911
4) Write a R program to get the unique elements of a given string and unique numbers of
vector.
Output:
[1] "original vector(string)"
[1] "The quick brown for jumps over the lazy dog."
[1] "unique elements of the said vector"
[1] "the quick brown for jumps over the lazy dog."
[1] "original vector(number)"
[1] 1 2 2 3 4 4 5 6
[1] "unique elements os the said vector:"
[1] 1 2 3 4 5 6
5) Write a R program to multiply two vectors of integers type and length 3.
Output:
[1] "Original Vectors are:"
[1] 10 20 30
[1] 20 10 40
[1] "Product of two Vectors:"
[1] 200 200 1200
6) Write a R program to list containing a vector, a matrix and a list and give names to the
elements in the list.
Output:
> source("~/.active-rstudio-document")
[1] "List:"
[[1]]
[1] "Red" "Green" "Black"
[[2]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
[[3]]
[[3]][[1]]
[1] "Python"
[[3]][[2]]
[1] "PHP"
[[3]][[3]]
[1] "Java"
$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"
$`Language(s)`[[2]]
[1] "PHP"
$`Language(s)`[[3]]
[1] "Java"
7) Write a R program to create a list containing a vector, a matrix and a list and give names
to the elements in the list. Access the first and second element of the list.
Output:
> source("~/.active-rstudio-document")
[1] "List:"
[[1]]
[1] "Red" "Green" "Black"
[[2]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
[[3]]
[[3]][[1]]
[1] "Python"
[[3]][[2]]
[1] "PHP"
[[3]][[3]]
[1] "Java"
$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"
$`Language(s)`[[2]]
[1] "PHP"
$`Language(s)`[[3]]
[1] "Java"
8) Write a R program to create a list containing a vector, a matrix and a list and remove the
second element.
Output:
> source("~/.active-rstudio-document")
[1] "List:"
[[1]]
[1] "Red" "Green" "Black"
[[2]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
[[3]]
[[3]][[1]]
[1] "Python"
[[3]][[2]]
[1] "PHP"
[[3]][[3]]
[1] "Java"
$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"
$`Language(s)`[[2]]
[1] "PHP"
$`Language(s)`[[3]]
[1] "Java"
$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"
$`Language(s)`[[2]]
[1] "PHP"
$`Language(s)`[[3]]
[1] "Java"
list1 = list(1,2,3)
list2 = list("Red", "Green", "Black")
print("Original lists:")
print(list1)
print(list2)
print("Merge the said lists:")
mlist = c(list1, list2)
print("New merged list:")
print(mlist)
Output:
> source("~/.active-rstudio-document")
[1] "Original lists:"
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[1]]
[1] "Red"
[[2]]
[1] "Green"
[[3]]
[1] "Black"
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Red"
[[5]]
[1] "Green"
[[6]]
[1] "Black"
10)Write a R program to assign new names "a", "b" and "c" to the elements of a given list.
Output:
> source("~/.active-rstudio-document")
[1] "Original list:"
$g1
[1] 1 2 3 4 5 6 7 8 9 10
$g2
[1] "R Programming"
$g3
[1] "Green"
[1] "Assign new names 'a', 'b' and 'c' to the elements of the said list"
$a
[1] 1 2 3 4 5 6 7 8 9 10
$b
[1] "R Programming"
$c
[1] "Green"
Output:
> source("~/.active-rstudio-document")
[1] "Empty dataframe"
[1] C1 C2 C3 C4 C5
<0 rows> (or 0-length row.names)
12) Write a R program to create a data frame from four given vectors.
Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT"))
print("Details of the employees:")
print(Employees)
Output:
> source("~/.active-rstudio-document")
[1] "Details of the employees:"
Name Gender Age Designation
1 Anastasia S M 23 Clerk
2 Dima R M 22 Manager
3 Katherine S F 25 Exective
4 JAMES A F 26 CEO
5 LAURA MARTIN M 32 ASSISTANT
13) Write a R program to create a data frame using two given vectors and display the
duplicated elements and unique rows of the said data frame.
a = c(10,20,10,10,40,50,20,30)
b = c(10,30,10,20,0,50,30,30)
print("Original data frame:")
ab = data.frame(a,b)
print(ab)
print("Duplicate elements of the said data frame:")
print(duplicated(ab))
print("Unique rows of the said data frame:")
print(unique(ab))
Output:
[1] "Original data frame:"
a b
1 10 10
2 20 30
3 10 10
4 10 20
5 40 0
6 50 50
7 20 30
8 30 30
[1] "Duplicate elements of the said data frame:"
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
[1] "Unique rows of the said data frame:"
a b
1 10 10
2 20 30
4 10 20
5 40 0
6 50 50
8 30 30
14) Write a R program to save the information of a data frame in a file and display the
information of the file.
exam_data = data.frame(
name = c('Amol', 'Diya', 'Karan', 'James', 'dinesh', 'Minnie', 'Megha', 'lila',
'Kevin', 'John'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)
print("Original dataframe:")
print(exam_data)
save(exam_data,file="data.rda")
load("data.rda")
file.info("data.rda")
Output:
> source("~/.active-rstudio-document")
[1] "Original dataframe:"
name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
15) Write a R program to create an ordered factor from data consisting of the names of
months.
Output:
> source("~/.active-rstudio-document")
[1] "March" "January" "May" "March" "August" "November" "June" "June"
"August"
[1] FALSE
[1] "August" "January" "June" "March" "May" "November"
17) Write R program to read number and print corresponding day name in a week.
Output:
Enter a number:(from 1 to 7) 4
4 is Wednesday
> source("~/.active-rstudio-document")
Enter a number:(from 1 to 7) 8
8 Invalid Day
> source("~/.active-rstudio-document")
Enter a number:(from 1 to 7) 0
0 Invalid Day
18) Create a Matrix using R and Perform the operations addition, subtraction, multiplication.
result = m1 + m2
print("Result of addition")
print(result)
result = m1 - m2
print("Result of subtraction")
print(result)
result = m1 * m2
print("Result of multiplication")
print(result)
Output:
[1] "Matrix-1:"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] "Matrix-2:"
[,1] [,2] [,3]
[1,] 0 2 0
[2,] 1 3 2
[1] "Result of addition"
[,1] [,2] [,3]
[1,] 1 5 5
[2,] 3 7 8
[1] "Result of subtraction"
[,1] [,2] [,3]
[1,] 1 1 5
[2,] 1 1 4
[1] "Result of multiplication"
[,1] [,2] [,3]
[1,] 0 6 0
[2,] 2 12 12
19) Using R import the data from Excel/.CSV file and find mean, median, mode, quartiles.
Notes: create a csv file in working directory. Check working directory by getwd().
Install package modest to calculate mode value.
library(modeest)
age_data<-read.csv("age_info.csv",header = TRUE,sep = ",")
print(age_data)
age_data
print("Mean value of age be:")
print(mean(age_data$Age))
print("Median value of data be:")
median = median(age_data$Height)
print(median)
print("Mode value of age be:")
mode=mfv(age_data$Age)
print(mode)
res<-quantile(age_data$Height, probs = c(0,0.25,0.5,0.75,1))
print(res)
Output:
Age Height
1 28 5.1
2 20 5.2
3 24 5.4
4 24 5.0
5 18 5.8
6 19 5.5
7 25 5.6
[1] "Mean value of age be:"
[1] 22.57143
[1] "Median value of data be:"
[1] 5.4
[1] "Mode value of age be:"
[1] 24
0% 25% 50% 75% 100%
5.00 5.15 5.40 5.55 5.80
20) Using R import the data from Excel/.CSV file and find standard deviation, variance and co-
variance.
Output:
Age Height
1 28 5.1
2 20 5.2
3 24 5.4
4 24 5.0
5 18 5.8
6 19 5.5
7 25 5.6
[1] "standard deviation value of age be:"
[1] 3.644957
[1] "variance value of age be:"
[1] 13.28571
[1] "co-variance value of age be:"
[1] -0.539378
21) Write a R program to count the number of NA values in a data frame column.
Output:
name marks attempts grades qualify
1 Manoj 12 1 A yes
2 Dinesh 14 2 A yes
3 Kartik 16 NA A yes
4 trupti 19 1 A yes
5 Disha 9 NA B no
6 Jonny 20 NA A yes
7 Amar 9 1 B no
[1] "The number of NA values in attempts column:"
[1] 3
22) Write a R program to call the (built-in) dataset air quality. Remove the variables 'Solar.R'
and 'Wind' and display the data frame.
data = airquality
print("Original data: Daily air quality measurements in New York, May to
September 1973.")
print(head(data, 10))
data[,c("Solar.R")]=NULL
data[,c("Wind")]=NULL
print("data.frame after removing 'Solar.R' and 'Wind' variables:")
print(head(data, 10))
Output:
> source("~/.active-rstudio-document")
[1] "Original data: Daily air quality measurements in New York, May to September
1973."
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9
10 NA 194 8.6 69 5 10
23) Write a R program to compare two data frames to find the row(s) in first data frame that
are not present in second data frame.
df_2019 = data.frame(
"item" = c("item1", "item2", "item3"),
"Jan_sale" = c(12, 14, 12),
"Feb_sale" = c(11, 12, 15),
"Mar_sale" = c(12, 14, 15)
)
df_2020 = data.frame(
"item" = c("item1", "item2", "item3"),
"Jan_sale" = c(12, 14, 12),
"Feb_sale" = c(11, 12, 15),
"Mar_sale" = c(12, 15, 18)
)
print("Original Dataframes:")
print(df_2019)
print(df_2020)
print("Row(s) in first data frame that are not present in second data
frame:")
print(setdiff(df_2019,df_2020))
Output:
> source("~/.active-rstudio-document")
[1] "Original Dataframes:"
item Jan_sale Feb_sale Mar_sale
1 item1 12 11 12
2 item2 14 12 14
3 item3 12 15 15
item Jan_sale Feb_sale Mar_sale
1 item1 12 11 12
2 item2 14 12 15
3 item3 12 15 18
[1] "Row(s) in first data frame that are not present in second data frame:"
$Mar_sale
[1] 12 14 15
24) Write a R program to create a factor corresponding to height of women data set, which
contains height and weights for a sample of women.
data = women
print("Women data set of height and weights:")
print(data)
height_f = cut(women$height,3)
print("Factor corresponding to height:")
print(table(height_f))
Output:
[1] "Women data set of height and weights:"
height weight
1 58 115
2 59 117
3 60 120
4 61 123
5 62 126
6 63 129
7 64 132
8 65 135
9 66 139
10 67 142
11 68 146
12 69 150
13 70 154
14 71 159
15 72 164
[1] "Factor corresponding to height:"
height_f
(58,62.7] (62.7,67.3] (67.3,72]
5 5 5
Output:
[1] "Original Vectors:"
[1] 10 20 30 20 20 25 9 26
[1] "nth highest value in a given vector:"
[1] "n = 1"
[1] 30
[1] "n = 2"
[1] 26
[1] "n = 3"
[1] 25
[1] "n = 4"
[1] 20
Output:
[1] "Original Vectors:"
[1] 10 20 30 25 9 26
[1] "Sort in ascending order:"
[1] 9 10 20 25 26 30
[1] "Sort in descending order:"
[1] 30 26 25 20 10 9
27) Write an R program to extract first 10 English letter in lower case and last 10 letters in
upper case and extract letters between 22nd to 24th letters in upper case.
Output:
> source("~/.active-rstudio-document")
[1] "First 10 Letters In Lower Case"
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
[1] "Last 10 letters In Upper Case"
[1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
[1] "Letters Between 22nd to 24th Letters In Upper Case"
[1] "V" "W" "X"
29) Write an R program to convert a given matrix to a list and print list in ascending order.
print("Original Matrix:")
print(my_matrix)
Output:
> source("~/.active-rstudio-document")
[1] "Original Matrix:"
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] "List from Matrix:"
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
[[5]]
[1] 5
[[6]]
[1] 6
[[7]]
[1] 7
[[8]]
[1] 8
[[9]]
[1]9
30) Write an R program to create a Data frames which contain details of 5employees and
display the details in ascending order.
Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT"),
SSN=c("123-34-2346","123-44-779","556-24-433","123-98-
987","679-77-576")
)
print("Details of the employees:")
print(Employees)
Output:
[1] "Details of the employees:"
Name Gender Age Designation SSN
1 Amit Kumar M 23 Clerk 123-34-2346
2 Raju Shaha M 22 Manager 123-44-779
3 Amar Pawar M 25 Exective 556-24-433
4 Dinesh k M 26 CEO 123-98-987
5 Vimala Mathur F 32 ASSISTANT 679-77-576
31) Consider the inbuilt iris dataset
i) Create a variable “y” and attach to it the output attribute of the “iris”dataset .
iii) Create a density plot matrix for each attribute by class value.
library(caret)
data(iris)
dataset<-iris
x <- dataset[,1:4]
y <- dataset[,5]
plot(y)
33) Write a script in R to create a list of students and perform the following
Output:
> source("~/.active-rstudio-document")
[[1]]
[1] "Roll.No : 101 Name : Alok Pawar"
[[2]]
[1] "Roll.No : 202 Name: Vandana More"
[[3]]
[1] "Roll.No : 303 Name : Tushar Sharma"
$SY
[1] "Roll.No : 202 Name: Vandana More"
$TY
[1] "Roll.No : 303 Name : Tushar Sharma"
$SY
[1] "Roll.No : 202 Name: Vandana More"
$TY
[1] "Roll.No : 303 Name : Tushar Sharma"
[[4]]
[1] "Roll.No : 404 Name : Dinkar Pratap"
$TY
[1] "Roll.No : 303 Name : Tushar Sharma"
[[3]]
[1] "Roll.No : 404 Name : Dinkar Pratap"
$TY
[1] "Roll.No : 505 Name : Shubhangi Gade"
[[3]]
[1] "Roll.No : 404 Name : Dinkar Pratap"