Dr. Mark Gardener

Home
About

Providing training for:

  • Ecology
  • Data analysis
  • Statistics
  • R The statistical programming language
  • Data management
  • Data mining

Tips and Tricks - for R and Excel

On this page you can find tips, tricks and hints for using both R and Excel. At the end of each tip there are links forwards and backwards as appropriate. There is also an index of R tips and an index of Excel tips.

For most analytical purposes the combination of Excel and R is unbeatable! Excel is great as a data management tool and for preparing data for analysis. You can also use it to get an overview of your data or to make simple (and not so simple) graphs. R is an analytical "swiss army knife" and can carry out a mind-boggling array of analytical routines as well as producing great graphics.

Tips & Tricks for R | Tips & Tricks for Excel | An Introduction to R | MonogRaphs | Writer's Bloc


Vector Objects
vector()
as.integer()

Top

Vector Objects

A vector is a one-dimensional object in R. There is no class specific for a vector but a vector object can be one of several classes, most usually:

  • numeric
  • character
  • logical
  • complex

Your numeric vector can be given as double or integer. Note that a factor is not considered to be a vector.

You can make a blank vector using the vector() command:

> vector(mode = "logical", length = 0)

For example:

> vl = vector(mode = "logical", length = 3)
> vl
[1] FALSE FALSE FALSE

> vc = vector(mode = "character", length = 5)
> vc
[1] "" "" "" "" ""

> vn = vector(mode = "numeric", length = 4)
> vn
[1] 0 0 0 0

> vi = vector(mode = "complex", length = 3)
> vi
[1] 0+0i 0+0i 0+0i

The blank vector you create can be "filled in" later:

> vi[1] = 3+2i
> vi
[1] 3+2i 0+0i 0+0i

Notice though that the value you enter can either be coerced to the appropriate form or alter the class of the vector:

> vc[1] = 23
> vc
[1] "23" "" "" "" ""

The value entered was a number (23) but the vector it is added to remains character in class. In the next example you create an integer vector:

> vi = vector(mode = "integer", length = 3)
> vi
[1] 0 0 0
> class(vi)
[1] "integer"

Adding a value can alter the class:

> vi[1] = 2
> vi
[1] 2 0 0
> class(vi)
[1] "numeric"

In this case the value (2) is taken as a regular double precision numeric value. To force the value to remain an integer you need to use the as.integer() command:

> vi = vector(mode = "integer", length = 3)
> vi[1] = as.integer(2)
> vi
[1] 2 0 0
> class(vi)
[1] "integer"

Now the entered value keeps its integer class.


Top << Previous Tip: Types of R object – 3. complex numbers >> Next Tip: Multi-dimensional objects in R
 
Follow me...
Facebook Twitter Google+ Linkedin Amazon
Top Tips & Tricks Home Index of R Tips Index of Excel Tips  
More links:

An introduction to R

See my Publications about Excel, R, statistics and data analysis Courses in R, data analysis, data management and statistics Visit the R Project website
 

See my Publications about statistics and data analysis.

MonogRaphs: random topics in R

Writer's Bloc – my latest writing project includes R scripts

Courses in data analysis, data management and statistics.


Top Home
Data Analysis
Contact GardenersOwn Homepage