A corner on convenient data analysis

January 24, 2013
By

(This article was first published on Burns Statistics » R language, and kindly contributed to R-bloggers)

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)

The post A corner on convenient data analysis appeared first on Burns Statistics.

To leave a comment for the author, please follow the link and comment on his blog: Burns Statistics » R language.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.