R melt for Google Apps Script and Excel/VBA: reshaping/unpivoting data

In Starter implementation of R-melt for Excel I introduced a one liner in Excel/VBA to transform this
into this

You could consider this to be a kind of unpivot – exploding tabulated data back into transactional rows as described by Hadley Wickhams in his implementation of reshape/melt for R.

Following a post by Tony Hirst I figured we could do with an Excel version, and now a Google Apps Script version. You’ll notice that I used more less the same javaScript syntax for options in both the VBA and GAS versions. I’ll probably be doing more of this in the future, so here is a quick write up on that.

I’m using the mcpher GAS libraries and in fact implemented this as a new module of those libraries. This means that all that is needed in the calling module for the example above, aside from a reference to the mcpher libraries, is this.
function testMelt(){
    mcpher.reshapeMelt ({outputSheet:”meltOut”,inputSheet:”melt”,id:[“id”,”time”]});
}

The code for this module is in this gist, the test spreadsheet here, and the complete library code here.

If you prefer Excel, the VBA example looks like this and can be downloaded here.
Public Sub testMelt()
    reshapeMelt “{‘outputSheet’:’meltOut’,’id’:[‘id’,’time’]}”
End Sub

For more stuff like this, visit the ramblings site or the associated blog. If you have suggestion for particular topic, vote for it on google moderator or contact me on our forum 

Author: brucemcp

Leave a Reply

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