R vectors

Vectors in R are analogous to Python lists - they are a data structure that contains an ordered list of values. A vector can be created using the following syntax:

vector <- c(<element1>, <element2>, <element3>)

Try entering the following in your R console:

> c(1,2,3,4,5,6)

It is also possible to create a vector consisting of a sequence of numbers using the following syntax:

sequence_of_numbers <- <start_number>:<finish_number>

Try entering the following into your R console:

> 1:5

Vectors can be indexed using integer numbers or other vectors. For example, the following creates a vector called my.vector, prints the third value of my.vector, and then prints the first five values of my.vector:

my.vector=1:10
print(my.vector)
print(my.vector[3])
print(my.vector[1:5])
Indexing in R starts with 1, so be sure not to get this mixed up with Python indexing, which starts at 0.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset