3) Create Vector via a Sequence.

This Blog entry is from the Data Structures section in Learn R.

There are two main ways to create vectors in a sequence of number in R, the first is using the semicolon in assignment, the second is using a function that does much the same while offering more flexibility.  The purpose of this procedure is to introduce some of the more sophisticated elements of the R language, however, for the purposes of predictive analytics it is not absolutely necessary to delve into such depth to achieve the end result of reliable predictive analytics.

To create a vector which is a sequence of numbers from 1 to 10, type:

SequenceBasic <- 1:10
script-to-use-colon-to-create-a-vector-by-sequence.png

Run the line of script to console:

vector-created-by-colon-sequence-in-r-console.png

It can be seen from the environment pane that the vector has been created and that the values span from 1 to 10 in increments of 1:

sequence-vector-written-to-rstudio-environment-variables.png

Introducing functions, the same using the seq() function can be achieved by typing:

SequenceFunction <- seq(1,10)
r-script-to-use-the-seq-function-to-create-vector.png

Run the line of script to console:

seq-function-written-out-to-r-console.png

It can be observed that SequenceBasic and SequenceFunction take the same form in the environment pane:

seq-function-vector-showing-in-rstudio-environment-window.png

The benefits of using the seq() function is that it allows for sequences to be created with different steps sizes,  where the default is 1.  To create a step of 0.25, type:

SequenceStep <= seq(1,10,0.25)
script-to-set-step-value-for-the-seq-function-in-r.png

Run the line of script to console:

stepped-seq-fuction-written-out-to-r-console.png

It can be seen that a much larger vector has been created by inspecting the environment pane, where the values increase by 0.25 increments:

stepped-seq-function-written-out-to-rstudio-environment-variables.png

The seq() function provides a lot of other options for the creation of sequences such as repetition which would be outside the scope of this procedure.  The seq() function has been introduced as a means to demonstrate assignment by function return values.