|A Programmer's Guide To R - The Vector|
|Written by Mike James|
|Monday, 10 April 2017|
Page 2 of 2
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.
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:
is 1 4 9 16
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:
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:
Many R functions accept vectors and process them in ways that you should find entirely obvious. For example:
results in a new window opening and a simple xy plot is drawn using the index as the x variable. Similarly:
creates true scatter plot.
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.
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:
You can see the attributes assigned to a list using the attributes function:
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:
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:
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.
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:
Also notice that a simple vector isn't a one-dimensional array because it doesn't have a defined dim attribute. That is:
is a one-dimensional vector but
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.
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:
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.
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.
A Programmers Guide To R
or email your comment to: email@example.com
|Last Updated ( Friday, 28 April 2017 )|