Publications by Lee Pang

A new R trick … for me at least

03.08.2013

What were going to be talking about today are dynamic argument lists for functions. Specifically, how to unpack and prepare them in R using ..., list(), and do.call()Biased by Matlab and varargin Initially, I based my use of ... in R on my experience with Matlab’s varargin. Using varargin, Matlab functions can have a signature of: function f(v...

3339 sym R (970 sym/12 pcs)

Groan – my first R package

20.08.2013

Being one of two R experts at my current job I figured I should be familiar with package development. Frankly, I’ve been procrastinating on this topic since I started using R in 2007 – I was doing just fine with source() and the section of the R manual on package development fell into the category of TL;DR. What finally drove me to learn the...

2980 sym R (803 sym/2 pcs) 6 img

mfg, mfcol, mfrow, and layout() – secret friends

26.08.2013

I was working on an issue (enhancement) today in my groan R-package today that required adding additional plotting elements via lines() and points() to a device that had already been partitioned by layout(). The code I wanted to use was essentially: # Y and S are lists of xy.coords() objects of the same length lyt = matrix(1:length(Y), ncol=10)...

1306 sym R (847 sym/5 pcs)

RegEx: Named Capture in R (Round 2)

09.10.2013

Previously, I came up with a solution to R’s less than ideal handling of named capture in regular expressions with my re.capture() function. A little more than a year later, the problem is rearing its ugly – albeit subtly different – head again. I now have a single character string: x = '`a` + `[b]` + `[1c]` + `[d] e`' from which I need to...

1402 sym R (1456 sym/6 pcs)

character handling: mean() vs sd()

21.10.2013

Here’s a weird R error/bug/nuance I came across today: What would you expect the following lines of code to return? x = c('1', '2', '3') mean(x) sd(x) Well, apparently it is: # mean(x) [1] NA # sd(x) [1] 1 So, sd() silently converts its input to numeric, while mean() does not. More evidence of this is in the source: > mean function (x, ...) ...

749 sym R (330 sym/3 pcs)

An R flaw: unexpected attribute droppings

06.02.2014

Today I was putting some code together that made plots from slices of a 3-dimensional array object aa. A couple of the dimensions in aa had names defined by named vectors. For example: > aa = array(runif(2*3*4), dim=c(2,3,4), dimnames=list(id = c(good='id1', evil='id2'), x = c(1,2,3), ...

1669 sym R (1742 sym/7 pcs)

Deploying Desktop Apps with R

02.04.2014

(Update) Despite the original publish date (Apr 1), this post was not and April Fools joke. I’ve also shortened the title a bit. As part of my job, I develop utility applications that automate workflows that apply more involved analysis algorithms. When feasible, I deploy web applications as it lowers installation requirements to simply a mo...

11452 sym R (3104 sym/17 pcs)

Optimizing with R expressions

21.08.2014

I recently discovered a powerful use for R expression()’sSay you are trying to fit some experimental data to the following nonlinear equation:Ky0eu(x−tl)K+y0(eu∗(x−tl)−1)+b1+(b0−b1)e−kx+b2xwith the independent variable x using nlminb() as the minimization optimizer.This sort of work is significantly improved (i.e. faster...

2885 sym R (4339 sym/10 pcs) 2 img

R OOP – a little privacy please?

23.08.2014

As of late, I’ve been making heavy use of Reference Classes in R. They are easier for me to wrap my mind around since they adopt a usage style more like “traditional” OOP languages like Java. Primarily, object methods are part of the class definition and accessed via the instantiated object.For instance: With S3/S4 classes, yo...

3791 sym R (711 sym/6 pcs)

Easy error propagation in R

22.01.2015

In a previous post I demonstrated how to use R’s simple built-in symbolic engine to generate Jacobian and (pseudo)-Hessian matrices that make non-linear optimization perform much more efficiently. Another related application is Gaussian error propagation. Say you have data from a set of measurements in variables x and y where you know the corr...

2904 sym R (3536 sym/10 pcs)