//
you're reading...
R, Uncategorized

Box Me

Here’s a short R function I wrote to turn a long data set into a wide one for viewing. It’s not the most exciting function ever but I find it quite useful when my screen is wide and short. It simply cuts the data set horizontally into equal size pieces and puts them side by side. Lazy I know!


#'boxMe
#'
#'Turns an overly long data frame into something easier to look at
#'
#' @param d A dataframe or matrix
#' @param nrow The number of rows you would like to see in the new dataframe
#' @examples
#' test.set<-data.frame(x=rnorm(100), y=rnorm(100))
#' boxMe(test.set, 18)
#' 
#' library(ggplot2)
#' boxMe(diamonds, 10)


boxMe<-function(d, nrow){
  
  # Number of rows and columns
  r<-dim(d)[1]
  c<-dim(d)[2]
  
  rem<-r %% nrow # Number of blank rows
  reps<-floor(r/nrow) # Number of folds
  s<-seq(1, reps*nrow, by=nrow) # Breaks
  
  box<-d[1:nrow,] # First col
  
  for (i in s[-1]){

    ap<-d[i:(i+nrow-1),]
    box<-cbind(box, ap)
    
  }
  
  #Append remainder
  
  if (rem>0){
  
    n.null.rows<-nrow-rem
    rem.rows<-d[(reps*nrow+1):r,]
    null.block<-as.data.frame(matrix(rep(NA, (n.null.rows*c)), nrow=n.null.rows))
    names(null.block)<-names(rem.rows)
    last.block<-rbind(rem.rows, null.block)
    box<-cbind(box, last.block)
  
  }

  return(box)

}

About these ads

About simonraper

I am an RSS accredited statistician with over 15 years’ experience working in data mining and analytics and many more in coding and software development. My specialities include machine learning, time series forecasting, Bayesian modelling, market simulation and data visualisation. As Data Scientist at Channel 4 my role is to develop machine learning solutions that allow the channel to build a deeper relationship with the viewer and innovate the way advertising is traded and work on supporting the creative side of the business. My current interests are in scalable machine learning (Mahout, Hadoop), interactive visualisatons (D3 and similar) and applying the methods of agile software development to analytics. I have worked for Mindshare, News International, Credit Suisse and AOL. I am co-author with Mark Bulling of Drunks and Lampposts - a blog on computational statistics, machine learning, data visualisation, R, python and cloud computing. It has had over 270 K visits and was mentioned in Flowing Data, I09, and the online editions of The New York Times and The New Yorker.

Discussion

Trackbacks/Pingbacks

  1. Pingback: Box Me - R Project Aggregate - January 1, 2014

Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Blog Stats

  • 288,084 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 480 other followers

Follow

Get every new post delivered to your Inbox.

Join 480 other followers

%d bloggers like this: