Archive for category: R language

Plot ranges of data in R

21 Feb 2013

How to control the limits of data values in R plots.

R has multiple graphics engines.  Here we will talk about the base graphics and the ggplot2 package.

We’ll create a bit of data to use in the examples:

one2ten <- 1:10

ggplot2 demands that you have a data frame:

ggdat <- data.frame(first=one2ten, second=one2ten)

Seriously exciting data, yes?

Default behavior

The default is — not surprisingly — to create limits so that the data comfortably fit. Read more →

R database interfaces

14 Feb 2013

Several packages on CRAN provide (or relate to) interfaces between databases and R.  Here is a summary, mostly in the words of the package descriptions.  Remember that package names are case-sensitive.

The packages that talk about being DBI-compliant are referring to the DBI package (see below in “Other SQL”).


dbConnect: Provides a graphical user interface to connect with databases that use MySQL.

RMySQL: The current version complies with the database interface definition as implemented in the package DBI 0.2-2.

TSMySQL: TSMySQL provides a MySQL interface for TSdbi. Comprehensive examples of all the TS* packages are provided in the vignette Guide.pdf with the TSdata package. Read more →

The three-dots construct in R

30 Jan 2013

There is a mechanism that allows variability in the arguments given to R functions.  Technically it is ellipsis, but more commonly called “…”, dots, dot-dot-dot or three-dots.


The three-dots allows:

  • an arbitrary number and variety of arguments
  • passing arguments on to other functions

Arbitrary arguments

The two prime cases are the c and list functions:

> c
function (..., recursive = FALSE)  .Primitive("c")
> list
function (...)  .Primitive("list")

Both of these allow you to give them as many arguments as you like, and you can name those arguments (which end up as names in the resulting object). Read more →

A corner on convenient data analysis

24 Jan 2013

Many people are of the opinion that R has a corner on convenient data analysis.  That may or may not be true.

But now R literally has a corner that makes data analysis more convenient.  If you have a data frame or a matrix with a few columns, then you can use head and/or tail to make sure that it looks as you expect.  However, the result is unappetizing if there are hundreds or thousands of columns.

That is is where corner comes in.  It shows you the first or last  few rows of the first or last few columns.

The  mtcars dataset can serve as an example even though it isn’t exactly a gigantic dataset.

By default the first 6 rows and first 6 columns are extracted:

> corner(mtcars)
                   mpg cyl disp  hp drat    wt
Mazda RX4         21.0   6  160 110 3.90 2.620
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875
Datsun 710        22.8   4  108  93 3.85 2.320
Hornet 4 Drive    21.4   6  258 110 3.08 3.215
Hornet Sportabout 18.7   8  360 175 3.15 3.440
Valiant           18.1   6  225 105 2.76 3.460

The same thing is done with: Read more →

© Copyright - Burns Statistics