A Programmer's Guide To R - The Vector

### New Book Reviews!

 A Programmer's Guide To R - The Vector
Written by Mike James
Monday, 10 April 2017
Article Index
A Programmer's Guide To R - The Vector
Vector Expressions & Attributes

## Vector Expressions

Now that you can create a vector, which for a beginner is best regarded as the fundamental R data type, you can start doing useful things.

For example, in R arithmetic is vector based.

For example:

`2*x`

is 2,4,6,8. Notice that 2 is a single element vector and this hence this is a vector times a vector.

You can combine general vectors in expressions. For example:

`x*x`

is 1 4 9 16

and

`x<-c(1,2,3,4)y<-c(4,3,2,1)``x+y[1] 5 5 5 5`

Of course, you can do what looks like scalar arithmetic if you want to and 2* 2 is still just 4, except of course it is actually a pair of single element vectors multiplied together.

It is also important to understand the way R will reuse a vector if it isn't large enough. For example, in:

`x<-c(1,2,3,4)y<-c(1,2)y*x[1] 1 4 3 8`

the y vector has only two elements, which are used in turn until all of the x vector elements have been multiplied.

The rule is that in vector arithmetic when a vector runs out of elements to be used in the expression the process continues from the first element until the longest vector has been processed.

Thus vector arithmetic always produces a vector equal to the longest vector in the expression.

Also notice that all R expression are vector expressions. For example:

`x<-c(1,2,3,4)y<-c(1,2)y==x``x[1] TRUE TRUE FALSE FALSE`

Many R functions accept vectors and process them in ways that you should find entirely obvious. For example:

`plot(x)`

results in a new window opening and a simple xy plot is drawn using the index as the x variable. Similarly:

`plot(x,y)`

creates true scatter plot.

## Attributes

All objects in R have an Attribute list and it is this that makes R data objects user friendly and give them some of the properties of a type based object system.

For example, every vector in R has an associated Attribute list that is in fact a slightly specialised form of another data structure the List - a parlist see later and the next article.

Basically Attributes are lists of name value pairs rather than just a simple indexed List.  Attributes serve to extend the way data works. There is a standard set of Attributes but you can add to this if you need to.

For example, the names attribute can be used to store a set of strings used to label the elements.  To set an attribute you can assign to the attr(object,attribute) function although R usually provides simpler and more natural ways of achieving the same result.

For example:

`x<-c(1,2.3)attr(x,"names") <-c("first","second","third")`

assigns the names first, second and third to each of the elements of the list. A more natural way of achieving the same result is:

`names(x)<-c("first","second","third")`

You can see the attributes assigned to a list using the attributes function:

`> attributes(x)\$names[1] "first"  "second" "third"`

However, attributes are often built into the natural behavior of functions and other facilities in R. For example, if a vector has a Names attribute you can use the names with the indexing operator - for example:

`> x[["first"]][1] 1`

In this case the Names attribute is searched for an item that matches and the corresponding element of the vector is returned.

As well as the Names attribute, there are some others that extend the behaviour of data structures:

• Names
• Dimensions
• Dimnames
• Classes
• Time series attributes

Attributes can be used to change the way that functions and the language treats particular data types. One of the best examples of how this works is the Dimensions attribute used with the vector - but keep in mind that attributes are very general in R.

## More Than One Dimension

R implements multi-dimensional arrays in a very primitive, but very flexible, way. All vectors are stored as a single-dimensional set of elements, but this can be treated as a set of rows and columns and so on to create a multi-dimensional array. To convert a vector to an array all you have to do is set the Dimensions attribute which specifies the size of the array in each of its dimensions.

For example:

`x<-c(1,2.3,4)dim(x)<- c(2,2)c[1,2]`

creates a vector with four elements 1 2 3 4. Setting the dim attribute to 2, 2 means that this four-element vector is to be treated as if it has two columns and two rows, i.e. a 2x2 matrix. Following this you can access elements using index notation like c[1,2], which gives the first row  and second column i.e. 3.

The array is stored in column major order, as per Fortran, and this means that we have the rows of the first column stored first. If you simply type x then the system will notice that a dim attribute has been set and print a two dimensional array:

`     [,1] [,2][1,]   1   3[2,]   2   4`

Also notice that a simple vector isn't a one-dimensional array because it doesn't have a defined dim attribute. That is:

`x<-c(1,2.3,4)`

is a one-dimensional vector but

`dim(x)<- c(4)`

converts it into a one-dimensional array. The difference is occasionally important.

In general, what data type something is depends on its structure and what attributes have been defined.

You can see that how a vector is treated as an array can be changed into an array with a different number of dimensions simply by changing the dim attribute. This is a very useful facility and when combined with the ability to use indexing you can easily create sub-vectors or sub-arrays.

To make things seem more like other languages. R provides some functions for creating Arrays without having to work directly with the Dim attribute.

For example:

`x<- array(c(1,2,3,4),c(2,2))`

creates the 2x2 array we have been using so far. If the data vector is smaller than the size of the array, its elements are recycled as in a general vector expression. For example:

`x<- array(0,c(2,2))`

is a common idiom for creating a zeroed array.

It is also worth mentioning at this early stage that a two-dimensional array is also a matrix and there are many functions and operators that only work on matrices.

## Where Next

Vectors are used often enough to handle simple computations, but R also introduces further specializations of the Vector to make data processing easier. For example, Data Frames (which are Matrices where each row can be contain different data types), i.e. a data table.

## Summary

• Everything in R is referred to as an object.

• All data in R consists of a header of metadata - the object's attributes - and the data structure itself.

• The fundamental data structure in R is the vector, which is essentially a one-dimensional array with attributes.

• Even the primitive data types in R are vectors. For example, 2 is a single-element vector.

• To reference a single vector element you use v[ [i] ] .

• To reference a sub vector you use v[i].

• For a vector v[i] and v[ [i] ] are almost the same thing as primitive data types are vectors.

• All arithmetic in R is vector-oriented.

• If a vector doesn't have enough elements in a vector expression, its elements are reused.

• Attributes can be used to change the way data structures are used by the system.

• The dim attribute can be used to interpret a one dimensional vector as an n dimensional array.

• A matrix is a 2 x 2 array.

• A one-dimensional vector is not the same as a one-dimensional array because it lacks a dim attribute.

A Programmers Guide To R

1. Getting Started And The Vector
2. Lists and more advanced data
3. Control
4. Functions
5. Working With Data

 Curie and Arduino 101 Abandoned By Intel26/07/2017Intel is discontinuing the Curie and the Curie-enabled Arduino 101. This has devastated developers who have based products on the Curie, but others are still unaware of the news, which has been releas [ ... ] + Full Story Breaking Fitness Records Without Even Moving - Spoofing The Fitbit29/06/2017Some new results indicate that it might be easy to falsify data gathered by even the best protected biometric device. Researchers have found two distinct ways of compromising a Fitbit.  + Full Story More News