A better head and tail method (for R)

Posted by

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)
  return(list)
}

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

Advertisements

5 comments

Comments are closed.