![]() |
VOOZH | about |
In this article, we are going how to create, modify, and access vectors in vector elements in the R Programming Language. Vector is a one-dimensional data structure that holds multiple data type elements.
It can be done in these ways:
The c() function in R Language is used to combine the arguments passed to it. We can create a vector by using c()
Syntax: c(value1,value2,........,value n)
Where, c stands for combine, values are the input data to the vector.
Output:
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
So in this program, we are going to create elements from 1 to 20 and display it
Now we are going to create a vector with different data type elements and display it.
Output:
[1] "1" "2" "3" "4" "5" "6" "7"
[8] "8" "9" "10" "11" "12" "13" "14"
[15] "15" "16" "17" "18" "19" "20" "sravan"
[22] "deepika" "jyothika" "45.7" "56" "34" "56"
Character type, float type, and int type are used. Int type data is created by using the range operator.
We can use : operator to creates the series of numbers in sequence for a vector.
Output:
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
seq() function is used to create a sequence of elements in a Vector. It takes the length and difference between values as optional argument.
Syntax: seq(from, to, by, length.out)
Parameters:
from: Starting element of the sequence
to: Ending element of the sequence
by: Difference between the elements
length.out: Maximum length of the vector
Output:
[1] 1 3 5 7 9
[1] 1.0 2.5 4.0 5.5 7.0 8.5 10.0First we generates a sequence of numbers from 1 to 10 with a step size (increment) of 2. then we generates a sequence of 7 numbers from 1 to 10, evenly spaced. It calculates the step size necessary to create a sequence with the specified length.
We can modify the vector elements by using [] operator
Syntax: vec[index_value]=new_data
Where index_value is the element index location
Output:
[1] 10 20 3 4 5
[1] 100 200 3 4 5In this example, we are going to create a vector with 5 integer type elements and modifying the 1st and 2nd elements as 100 and 200
Output:
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[1] 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120In this example, we are going to modify the elements from 1 to 20 with 1010 to 120 at a time.
We can access vector elements using the indexing operator - [] and Indexing starts from 1.
Syntax: vector_name[index_value]
Output:
[1] 100
[1] 111
[1] 112In this example, we are going to create the vector with elements from 100 to 200 using range operator and going to access the 1st, 12th and 13th elements.
Output:
[1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
[21] 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
[41] 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
[61] 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
[81] 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199In this example, we created a vector with integer data type elements from 100 to 199, with range operator and Access all.