This repository was archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path20.Access_and_Modify_Factor.r
More file actions
70 lines (56 loc) · 1.62 KB
/
20.Access_and_Modify_Factor.r
File metadata and controls
70 lines (56 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Aim: Write an R Program to Access and Modify Components of a Factor.
# Source Code:
fac1<-function()
{
branch=c("CSE","MEC","ECE","CSE","MEC","MEC", "CSE","MEC","MEC","CSE")
REC=factor(branch)
cat("The variable branch as a factor variable is:\n")
print(REC)
cat("\nThe structure of the factor variable is:\n")
print(str(REC))
cat("\nThe 3rd component of the factor variable is:\n")
print(REC[3])
cat("\nThe 1st and 4th components of the factor variable is:\n")
print(REC[c(1,4)])
cat("\nModifying the factor 3rd component as MEC:\n")
REC[3]="MEC"
REC=factor(REC)
print(REC)
cat("\nThe levels of the factor are:\n")
print(levels(REC))
cat("\nAdding a new level EEE to the factor levels\n")
levels(REC)=c(levels(REC),"EEE")
cat("The updated levels are:\n")
print(levels(REC))
}
fac1()
# OUTPUT:
# The variable branch as a factor variable is:
# [1] CSE MEC ECE CSE MEC MEC CSE MEC MEC CSE
# Levels: CSE ECE MEC
#
# The structure of the factor variable is:
# Factor w/ 3 levels "CSE","ECE","MEC": 1 3 2 1 3 3 1 3 3 1
# NULL
#
# The 3rd component of the factor variable is:
# [1] ECE
# Levels: CSE ECE MEC
#
# The 1st and 4th components of the factor variable is:
# [1] CSE CSE
# Levels: CSE ECE MEC
#
# Modifying the factor 3rd component as MEC:
# [1] CSE MEC MEC CSE MEC MEC CSE MEC MEC CSE
# Levels: CSE MEC
#
# The levels of the factor are:
# [1] "CSE" "MEC"
#
# Adding a new level EEE to the factor levels
# The updated levels are:
# [1] "CSE" "MEC" "EEE"
# Bonus :
# Total deletion fo variables
# rm(list=ls())