A corner on convenient data analysis

24 Jan 2013
2013/01/24

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:

> corner(mtcars, 'tl')
                   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 “t” stands for “top” and the “l” stands for “left”.  You can get the last 6 rows and last 6 columns with:

> corner(mtcars, 'br')
                  wt qsec vs am gear carb
Porsche 914-2  2.140 16.7  0  1    5    2
Lotus Europa   1.513 16.9  1  1    5    2
Ford Pantera L 3.170 14.5  0  1    5    4
Ferrari Dino   2.770 15.5  0  1    5    6
Maserati Bora  3.570 14.6  0  1    5    8
Volvo 142E     2.780 18.6  1  1    4    2

As in “bottom right”.

Get the first 3 rows and the last 3 columns with:

> corner(mtcars, 'tr', n=3)
              am gear carb
Mazda RX4      1    4    4
Mazda RX4 Wag  1    4    4
Datsun 710     1    4    1

Get the last 4 rows and the first 5 columns with:

> corner(mtcars, 'bl', n=c(4,5))
                mpg cyl disp  hp drat
Ford Pantera L 15.8   8  351 264 4.22
Ferrari Dino   19.7   6  145 175 3.62
Maserati Bora  15.0   8  301 335 3.54
Volvo 142E     21.4   4  121 109 4.11

It works with higher dimensional arrays as well.

Getting it

Of course before you can use it, you have to have the package installed on your machine.  Since the package is on CRAN, you only need to do:

install.packages("BurStMisc")

assuming you have an internet connection and there are no firewall issues.

In every R session that you want to use the package, you need to do:

require(BurStMisc)
5 replies
  1. David Kane says:

    Cool! Any chance of getting that into base R? (I seem to recall that head() and tail() started as functions in someone’s personal package before they were moved in base R)

    Reply
    • Patrick Burns says:

      David,
      corner was right next to head and tail and only the latter two went into the utils package. I’m guessing that you’ll just have to live with the status quo, but I wouldn’t think it impossible (and I’d welcome) that corner move in with its old friends.

      Reply
  2. PAC says:

    That’s nice but my dream is to have a command similar to the `less` command line in Unix which makes it easy to browse the data block by block !

    Reply
    • Patrick Burns says:

      I think you are looking for the ‘page’ function:

      page(as.matrix(1:1000), method=’print’)

      Reply

Trackbacks & Pingbacks

  1. […] a package to the search list with the require function.  For example to add the BurStMisc package to the search list, you would […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright - Burns Statistics