-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector Data Structure.R
132 lines (103 loc) · 2.67 KB
/
Vector Data Structure.R
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#Vector :
#1 DS
#contains homogeneous elements
#types of vector : integer , complex , numeric , character , logical
#create vector : by using : , c() , seq() , assign() , rep()
#common properties of vector :
#length() : returns lenght of vector
#typeof() : returns vector type
#attributes() : returns names of element in the vector
#is() : used to check whether vector is specified type
#class() : is used to identify the type of vector or object
#Dealing with NA values : is.na() , na.omit()
#sorting : sort()
#vector indexing : single Element indexing , multiple element indexing , negative indexing
#vector modify
#vector delete : negative index , rm()
#scan()
#creation of vector
#1.sigle elemnt vector :
v1 <- "fruit"
class(v1)
typeof(v1)
length(v1)
is.numeric(v1)
is.na(v1)
na.omit(v1)
#2. using : (colon operator):
v2 <- 1:10
v2
#3. seq()
#odd numeric vector
v3 <- seq(1 , by = 2 , length.out = 3 )
v3
#even numeric vector
v4 <- seq(2 , by = 2 , length.out = 4)
v4
#using from & to
v5 <- seq(1 , 10 , by = 2)
v5
#4.assign()
#syntax :
assign("VariableName" , data)
assign("v6" , 1:10)
v6
assign("v7" , c(10 , 20 , 30))
v7
#5.using c()
v8 <- c(v6 , v7)
v8
#unique is used to avoid redundency of element
v8 <- unique(v8)
v8
#6.rep()
rep(c(1,2,3) , time = 2)
rep(c(1,2,3) , each = 2)
#access elements of vector :
#vector indexing :
#signle element indexing
v8[2]
names(v8)[c(1,2,3)] <- c("First","Second","Third")
v8
v8["First"]
attributes(v8)
#update vector elements
v10
v10[1]<- 10
v10
names(v10)[c(2,3)] <- c("second" , "third")
attributes(v10)
v10["second"] <- 20
#multiple element indexing
v8[c(2,4,6)]
#negative indexing : delete elements in the vector
v8[c(-1 , -6)]
#rm() is used to delete elements
rm(v7)
v8 <- NULL
v8
#dealing with NA values
v9 <- c(1 , 2, NA , NA , 3)
is.na(v9)
v9 <- as.numeric(na.omit(v9))
v9
#sorting : sort()
v10 <- seq(0 , by = 2 , length.out = 10)
v10
sort(v10 , decreasing = TRUE)
sort(v10)
#scan() : it is built-in function which is used to take inputs from the user or console.
#by using this function we can read data from the user or file also
#syntax :
scan(file = "" , what = dataType , n = -1 )
#parameter :
file = this parameter is used to specify name of the file from which the data is to be read
what = it is used to specify what type of data should be used
n = is used to specify the no of element to be read or -1 mean read all available data
#example :
v11 <- scan(what = integer() , n = 3)
v11
v12 <- scan(what = character() , n = 3)
v12
v13 <- readline("Enter the no : ")
class(v13)