[go: up one dir, main page]

0% found this document useful (0 votes)
5 views29 pages

Practical Programs (1)

The document contains a series of R programming exercises designed for a practical introduction to data science. It includes tasks such as taking user input, creating sequences, generating plots, manipulating lists, and working with data frames. Each exercise is accompanied by example code and expected output.

Uploaded by

truptinik27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views29 pages

Practical Programs (1)

The document contains a series of R programming exercises designed for a practical introduction to data science. It includes tasks such as taking user input, creating sequences, generating plots, manipulating lists, and working with data frames. Each exercise is accompanied by example code and expected output.

Uploaded by

truptinik27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

M.

Sc(Computer Application)

CA 555 MJP: Practical Introduction to Data Science

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.

name = readline(prompt="Input your name: ")


age = readline(prompt="Input your age: ")
print(paste("My name is",name, "and I am",age ,"years old."))
print(R.version.string)

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.

print("Sequence of numbers from 20 to 50:")


print(seq(20,50))

print("Mean of numbers from 20 to 60:")


print(mean(20:60))

print("Sum of numbers from 51 to 91:")


print(sum(51: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

3) Write a R program to create a simple bar plot of five subjects marks.

marks = c(70, 95, 80, 74)


barplot(marks,
main = "Comparing marks of 5 subjects",
xlab = "Marks",
ylab = "Subject",
names.arg = c("English", "Science", "Math.", "Hist."),
col = "darkred",
horiz = FALSE)

4) Write a R program to get the unique elements of a given string and unique numbers of
vector.

str1="The quick brown for jumps over the lazy dog."


print("original vector(string)")
print(str1)
print("unique elements of the said vector")
print(unique(tolower(str1)))
nums=c(1,2,2,3,4,4,5,6)
print("original vector(number)")
print(nums)
print("unique elements os the said vector:")
print(unique(nums))

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.

A = c(10, 20, 30)


B = c(20, 10, 40)
print("Original Vectors are:")
print(A)
print(B)
print("Product of two Vectors:")
C=A*B
print(C)

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.

list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2),


list("Python", "PHP", "Java"))
print("List:")
print(list_data)
names(list_data) = c("Color", "Odd numbers", "Language(s)")
print("List with column names:")
print(list_data)

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"

[1] "List with column names:"


$Color
[1] "Red" "Green" "Black"

$`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.

list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2),


list("Python", "PHP", "Java"))
print("List:")
print(list_data)
names(list_data) = c("Color", "Odd numbers", "Language(s)")
print("List with column names:")
print(list_data)
print('1st element:')
print(list_data[1])
print('2nd element:')
print(list_data[2])

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"

[1] "List with column names:"


$Color
[1] "Red" "Green" "Black"

$`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"

[1] "1st element:"


$Color
[1] "Red" "Green" "Black"

[1] "2nd element:"


$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11

8) Write a R program to create a list containing a vector, a matrix and a list and remove the
second element.

list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2),


list("Python", "PHP", "Java"))
print("List:")
print(list_data)
names(list_data) = c("Color", "Odd numbers", "Language(s)")
print("List with column names:")
print(list_data)
print('list after removing second element:')
list_data[2]=NULL
print(list_data)

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"

[1] "List with column names:"


$Color
[1] "Red" "Green" "Black"

$`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"

[1] "list after removing second element:"


$Color
[1] "Red" "Green" "Black"

$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"

$`Language(s)`[[2]]
[1] "PHP"

$`Language(s)`[[3]]
[1] "Java"

9) Write a R program to merge two given lists into one list.

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"

[1] "Merge the said lists:"


[1] "New merged list:"
[[1]]
[1] 1

[[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.

list1 = list(g1 = 1:10, g2 = "R Programming", g3 = "HTML")


print("Original list:")
print(list1)
names(list1) = c("a", "b", "c")
print("Assign new names 'one', 'two' and 'three' to the elements of the said
list")
print(list1)

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"

11) Write a R program to create an empty data frame.

# declaring an empty data frame with 5


# columns and null entries
df = data.frame(matrix(
vector(), 0, 5, dimnames=list(c(), c("C1","C2","C3","C4","C5"))),
stringsAsFactors=F)
# printing the empty data frame
print ("Empty dataframe")
print (df)

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.

Employees = data.frame(Name=c("Anastasia S","Dima R","Katherine S",


"JAMESA","LAURA MARTIN"),
Gender=c("M","M","F","F","M"),
Age=c(23,22,25,26,32),

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.

x <- c("March", "January", "May", "March",


"August", "November", "June", "June", "August")
print(x)
print(is.factor(x))
# Apply the factor function.
factor_x = factor(x)
print(levels(factor_x))

Output:
> source("~/.active-rstudio-document")
[1] "March" "January" "May" "March" "August" "November" "June" "June"
"August"
[1] FALSE
[1] "August" "January" "June" "March" "May" "November"

16) Write R program to find whether given number is positive or negative.

number <- as.numeric(readline(prompt = "Enter a number: "))

# Determine and display whether the number is positive, negative, or zero


if (number > 0) {
cat("The number is positive.")
} else if (number < 0) {
cat("The number is negative.")
} else {
cat("The number is zero.")
}
Output:
Enter a number: 25
The number is positive.
> source("~/.active-rstudio-document")
Enter a number: -9
The number is negative.

17) Write R program to read number and print corresponding day name in a week.

day <- as.numeric(readline(prompt = "Enter a number:(from 1 to 7) "))

# Determine and display whether the number is positive, negative, or zero


if (day ==1 ) {
cat(day, "is Sunday")
} else if (day ==2 ) {
cat(day, "is Monday")
} else if (day ==3 ) {
cat(day, "is Saturday")
} else if (day ==4 ) {
cat(day, "is Wednesday")
} else if (day ==5 ) {
cat(day, "is Thursday")
} else if (day ==6 ) {
cat(day, "is Friday")
} else if (day ==7 ) {
cat(day, "is Saturday")
} else {
cat(day, "Invalid Day")
}

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.

# Create two 2x3 matrixes.


m1 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
print("Matrix-1:")
print(m1)
m2 = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
print("Matrix-2:")
print(m2)

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.

#find standard deviation, variance and co-variance.


age_data<-read.csv("age_info.csv",header = TRUE,sep = ",")
print(age_data)
print("standard deviation value of age be:")
print(sd(age_data$Age))
print("variance value of age be:")
print(var(age_data$Age))
print("co-variance value of age be:")
#co variance require both x and y
print(cor(age_data$Age, age_data$Height))

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.

name = c('Manoj', 'Dinesh', 'Kartik', 'trupti', 'Disha','Jonny', 'Amar')


marks = c(12, 14, 16, 19, 9, 20, 9)
attempts=c(1,2,NA,1,NA,NA,1)
grades = c('A', 'A', 'A', 'A', 'B', 'A', 'B')
qualify = c('yes', 'yes', 'yes', 'yes', 'no', 'yes', 'no')
exam_data = data.frame(name, marks,attempts, grades, qualify)
print(exam_data)
print("The number of NA values in attempts column:")
print(sum(is.na(exam_data$attempts)))

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

[1] "data.frame after removing 'Solar.R' and 'Wind' variables:"


Ozone Temp Month Day
1 41 67 5 1
2 36 72 5 2
3 12 74 5 3
4 18 62 5 4
5 NA 56 5 5
6 28 66 5 6
7 23 65 5 7
8 19 59 5 8
9 8 61 5 9
10 NA 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

25) Write a R program to find nth highest value in a given vector.

x = c(10, 20, 30, 20, 20, 25, 9, 26)


print("Original Vectors:")
print(x)
print("nth highest value in a given vector:")
print("n = 1")
n=1
print(sort(x, TRUE)[n])
print("n = 2")
n=2
print(sort(x, TRUE)[n])
print("n = 3")
n=3
print(sort(x, TRUE)[n])
print("n = 4")
n=4
print(sort(x, TRUE)[n])

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

26) Write an R program to sort a Vector in ascending and descending order.

x = c(10, 20, 30, 25, 9, 26)


print("Original Vectors:")
print(x)
print("Sort in ascending order:")
print(sort(x))
print("Sort in descending order:")
print(sort(x, decreasing=TRUE))

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.

print("First 10 Letters In Lower Case")


Z = head(letters,10)
print(Z)
print("Last 10 letters In Upper Case")
Z = tail(LETTERS,10)
print(Z)
print("Letters Between 22nd to 24th Letters In Upper Case")
ZS = tail(LETTERS[22:24])
print(ZS)

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"

28) Write an R Program to calculate Decimal into binary of a given number.

convert_to_binary <- function(n) {


if(n > 1) {
convert_to_binary(as.integer(n/2))
}
cat(n %% 2)
}
Output:
> convert_to_binary(13)
1101

29) Write an R program to convert a given matrix to a list and print list in ascending order.

my_matrix <- matrix(1:9, nrow = 3)

print("Original Matrix:")
print(my_matrix)

# Convert matrix to list


my_list <- as.list(my_matrix)

# Print converted list


print("List from Matrix:")
print(my_list)

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.

Employees = data.frame(Name=c("Amit Kumar","Raju Shaha","Amar


Pawar", "Dinesh k","Vimala Mathur"),
Gender=c("M","M","M","M","F"),
Age=c(23,22,25,26,32),

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 .

ii) Create a barplot to breakdown your output attribute.

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)

scales <- list(x=list(relation="free"), y=list(relation="free"))


featurePlot(x=x, y=y, plot="density", scales=scales)
32) Consider Weather dataset

i) Selecting using the column number

ii) Selecting using the column name

iii) Make a scatter plot to compare Wind speed and temperature.

33) Write a script in R to create a list of students and perform the following

i) Give names to the students in the list.

ii) Add a student at the end of the list.

iii) Remove the first Student.

iv) Update the second last student.

students=list(c("Roll.No : 101 Name : Alok Pawar"),


c("Roll.No : 202 Name: Vandana More"),
c("Roll.No : 303 Name : Tushar Sharma"))
print("list of the students: ")
print(students)
print("After naming to the list elements:")
names(students)=list("FY","SY","TY")
print(students)
students[4]="Roll.No : 404 Name : Dinkar Pratap"
print("Student Added At The End Of List :")
print(students)
#Remove the first Student.
students[1]=NULL
print("First Student Removed From List :")
print(students)
#Update the second last student.
students[2]="Roll.No : 505 Name : Shubhangi Gade"
print("Second Last Student Updated In List :")
print(students)

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"

[1] "After naming to the list elements:"


$FY
[1] "Roll.No : 101 Name : Alok Pawar"

$SY
[1] "Roll.No : 202 Name: Vandana More"

$TY
[1] "Roll.No : 303 Name : Tushar Sharma"

[1] "Student Added At The End Of List :"


$FY
[1] "Roll.No : 101 Name : Alok Pawar"

$SY
[1] "Roll.No : 202 Name: Vandana More"

$TY
[1] "Roll.No : 303 Name : Tushar Sharma"

[[4]]
[1] "Roll.No : 404 Name : Dinkar Pratap"

[1] "First Student Removed From List :"


$SY
[1] "Roll.No : 202 Name: Vandana More"

$TY
[1] "Roll.No : 303 Name : Tushar Sharma"

[[3]]
[1] "Roll.No : 404 Name : Dinkar Pratap"

[1] "Second Last Student Updated In List :"


$SY
[1] "Roll.No : 202 Name: Vandana More"

$TY
[1] "Roll.No : 505 Name : Shubhangi Gade"

[[3]]
[1] "Roll.No : 404 Name : Dinkar Pratap"

You might also like