How to create vector in R with a sequence of numbers?

R ProgrammingServer Side ProgrammingProgramming

Creating a numeric vector is the first step towards learning R programming and there are many ways to do that but if we want to generate a sequence of number then it is a bit different thing, not totally different. We can create a vector with a sequence of numbers by using − if the sequence of numbers needs to have only the difference of 1, otherwise seq function can be used.

Example

 Live Demo

> x1<-1:50
> x1

Output

[1] 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] 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

Example

 Live Demo

> x2<-50:1
> x2

Output

[1] 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
[26] 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Example

 Live Demo

> x3<-seq(1,100,by=5)
> x3

Output

[1] 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96

Example

 Live Demo

> x4<-seq(1,500,by=5)
> x4

Output

[1] 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86
[19] 91 96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176
[37] 181 186 191 196 201 206 211 216 221 226 231 236 241 246 251 256 261 266
[55] 271 276 281 286 291 296 301 306 311 316 321 326 331 336 341 346 351 356
[73] 361 366 371 376 381 386 391 396 401 406 411 416 421 426 431 436 441 446
[91] 451 456 461 466 471 476 481 486 491 496

Example

 Live Demo

> x5<-seq(5,500,by=5)
> x5

Output

[1] 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90
[19] 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180
[37] 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270
[55] 275 280 285 290 295 300 305 310 315 320 325 330 335 340 345 350 355 360
[73] 365 370 375 380 385 390 395 400 405 410 415 420 425 430 435 440 445 450
[91] 455 460 465 470 475 480 485 490 495 500

Example

 Live Demo

> x6<-seq(-500,5,by=5)
> x6

Output

[1] -500 -495 -490 -485 -480 -475 -470 -465 -460 -455 -450 -445 -440 -435 -430
[16] -425 -420 -415 -410 -405 -400 -395 -390 -385 -380 -375 -370 -365 -360 -355
[31] -350 -345 -340 -335 -330 -325 -320 -315 -310 -305 -300 -295 -290 -285 -280
[46] -275 -270 -265 -260 -255 -250 -245 -240 -235 -230 -225 -220 -215 -210 -205
[61] -200 -195 -190 -185 -180 -175 -170 -165 -160 -155 -150 -145 -140 -135 -130
[76] -125 -120 -115 -110 -105 -100 -95 -90 -85 -80 -75 -70 -65 -60 -55
[91] -50 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5

Example

 Live Demo

> x7<-seq(-500,-1,by=5)
> x7

Output

[1] -500 -495 -490 -485 -480 -475 -470 -465 -460 -455 -450 -445 -440 -435 -430
[16] -425 -420 -415 -410 -405 -400 -395 -390 -385 -380 -375 -370 -365 -360 -355
[31] -350 -345 -340 -335 -330 -325 -320 -315 -310 -305 -300 -295 -290 -285 -280
[46] -275 -270 -265 -260 -255 -250 -245 -240 -235 -230 -225 -220 -215 -210 -205
[61] -200 -195 -190 -185 -180 -175 -170 -165 -160 -155 -150 -145 -140 -135 -130
[76] -125 -120 -115 -110 -105 -100 -95 -90 -85 -80 -75 -70 -65 -60 -55
[91] -50 -45 -40 -35 -30 -25 -20 -15 -10 -5

Example

 Live Demo

> x8<-seq(-50,50,by=2)
> x8

Output

[1] -50 -48 -46 -44 -42 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14
[20] -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24
[39] 26 28 30 32 34 36 38 40 42 44 46 48 50

Example

 Live Demo

> x9<-seq(500,5000,by=100)
> x9

Output

[1] 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900
[16] 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000 3100 3200 3300 3400
[31] 3500 3600 3700 3800 3900 4000 4100 4200 4300 4400 4500 4600 4700 4800 4900
[46] 5000
raja
Published on 03-Sep-2020 11:13:11
Advertisements