A Programmer's Guide to R - Data and Objects

### New Book Reviews!

 A Programmer's Guide to R - Data and Objects
Written by Mike James
Monday, 13 December 2010
Article Index
A Programmer's Guide to R - Data and Objects
Attributes
Vectors

## The vector

All data structures in R are examples of Lists. That is, a List is a very general data structure capable of representing anything you care to use it for. This is powerful but it can be too complicated for the beginner. To make things simple R places restrictions on the structure of a List and uses the Classes attribute to make them work as if they were different data types.

For example, the vector is a List that is composed of elements that are all of the same basic type  - R tends to use the term "mode" rather then type but they almost mean the same thing.

There is a distinction between an object's type and its mode - the type of its elements. For example x<-c(1,2,3,4) creates x which is a vector with mode numeric.

Being a statistical language R relaxes this slightly in that there is a NA or Not Available type that can be used anywhere to indicate a missing value.

The class of an object is used by functions to work out how to treat the object or to test to see if it is suitable for processing. For example, the c function when used with numeric values creates a vector with class set to "numeric". This is an indication to any function that processes the new data structure that all of its elements are numeric and so can be used in arithmetic. A numeric vector is still a List but with its Class attribute set to "numeric".

There are conversion functions that take a general object a List say and will set its class property if it satisfies the conditions. For example, a List, x say, that has elements that are all of the same type satisfies the condition to be a vector and so you can convert it using as.vector. For example:

`> lis=list(1,2,3)> class(lis)[1] "list"`
`vec<-as.vector(lis,mode="numeric")`
`> class(vec)[1] "numeric"`

You could also change the class of lis using:

`attr(lis,"class")<-c("numeric")`

lis is now a vector.

This basic idea - that a List can be used to represent other simpler data structures by restricting it and indicating to functions how it should be treated is R's way of making things simpler. Let's see just how simple by looking at what a user can do with a vector.

## Working with vectors

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.

`x*x`

is 1 4 9 16

and

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

Of course you can do scalar arithmetic if you want to and 2* 2 is still just 4.

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.

You can see that for the beginner the vector really is the best place to start, but we need to keep in mind that it is just a list that has elements all of the same type.

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

More of this and using R instructions and expressions in a future article.

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter.

Last Updated ( Friday, 02 September 2016 )