R: Creating Sequences


I. Using ":"

Using ":", one is able to quickly create a sequence of numbers, incremented by 1.

> # CREATE A SEQUENCE FROM 1 TO 5, BY INCREMENTS OF 1
> 1:5
[1] 1 2 3 4 5
> 
> # CREATE A SEQUENCE FROM 1 TO 8, ASSIGN TO stats.data VARIABLE 
> one_eight = 1:8 
> 
> # VIEW DATA IN VARIABLE stats.data
> one_eight
[1] 1 2 3 4 5 6 7 8

Create a list of numbers 5 through -6, assign the name decreasing.data to your data.

> #this code will create a sequence of data from 5 to -6, by increments of 1 
> decreasing_data = 5:-6
> decreasing_data
 [1]  5  4  3  2  1  0 -1 -2 -3 -4 -5 -6

 


II. Using seq() Function

The seq(from, to, by) function creates a sequence of numbers between any two numbers incremented by a specific number. Below is the code to create a sequence of numbers from 1 through 3, with increments of 0.5.
> # seq(low.number, high.number, increment.size)
> z = seq(from = 1,to = 3,by = 0.5)  
> z
[1] 1.0 1.5 2.0 2.5 3.0

Also, the seq(from, to, length.out) function creates a sequence of numbers with a specific length between any two numbers. Below is code to create a sequence of numbers with length 6 from 0 through 12

> seq(from = 0,to = 12,length.out = 6)
[1]  0.0  2.4  4.8  7.2  9.6 12.0

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.