A better head and tail method (for R)

The head and tail method I posted a few days back runs into problems with matrices and data-frames (due to my use of cat()), and doesn’t tell you what it’s publishing when it shows you head and tail.

With some help from stack overflow I have now fixed those bugs …

ht <- function(d, m=5, n=m){
  # print the head and tail together
  list <- NULL
  list[[paste0('HEAD #', m)]] <- head(d,m)
  list[[paste0('TAIL #', n)]] <- tail(d,n)

Note, the return value is now the list – rather than NULL. For example:

> x <- 1:100
> v <- ht(x)
> str(v)
List of 2
 $ HEAD #5: int [1:5] 1 2 3 4 5
 $ TAIL #5: int [1:5] 96 97 98 99 100

This entry was posted in R and tagged . Bookmark the permalink.

5 Responses to A better head and tail method (for R)

  1. Manny C says:

    Glad you’re enjoying it buddy. I’m using your function.

  2. Manny says:

    PS – If you ever package this up, you might want to use these Hadley tool:

  3. Pingback: Creating an R package (in 10 mins) | ricardian ambivalence

please comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s