Use the superassignment operator

One of the most important functional programming principle is that functions do not change non-local variables; that is, generally speaking, the code in a function only has read access to its non-local variables.  This is a quite important feature which can protect the higher-level variable from being changed by local functions. See the example below.

> x <- 10
> test <- function(x) {
+   x <- x - 5
+   print(x)
+ }
> test(x)
[1] 5
> x
[1] 10

However, sometimes you may wish to write to a global variable or any variable higher than the level at which your write statement exists. The superassignment operator, <<-, or the assign() function is what you want. Let’s look at the superassignment operator first.Continue reading

get() function in R

The get() function might be one of the most useful utilities in R. However I’ve never use this function before I write this page. Shame on me.

Well, let’s get to the point. The job of the get() function is actually quite simple: given the name of an object, it fetches the object itself. See the example below:

> x <- c(1:3)
> x
[1] 1 2 3
> get("x")
[1] 1 2 3

It’s easy to imagine how useful this function is.


Reference: The Art of R Programming by Norman Matloff

Use SAS system options to suppress Log output

For Windows SAS system, the Output, Log, and Program Editor windows can display approximately 99,999 lines each. Since the number is limited, sometimes the SAS Log windows will be full and the system will show a messages ‘SAS log window is full’. The running program will pause and wait for your action. It’s quite annoying, especially when you run a program with hundreds or thousands of loops.

I run a simulation program with a loop that went 100,000 times recently and it took more than 4 hours to run, even on the sever. And I want to suppress the log output because I don’t want that message to interrupt the program running.

I read the SAS documents carefully, and there are several option available to prevent the log window from filling up.

Solution 1: System options

There are four system options can be used to suppress SAS statements, system messages, and error message, respectively.Continue reading

The difference between using subset() function and ordinary filtering

Well at first, I thought there is no difference between this two methods. And I normally use  these two methods interchangeable when I wrote the R code.

And actually there  is a small difference in how NA values are handled.

> x <- c(6, 1, NA, 10)
> x
[1]  6  1 NA 10
> x[x > 5]
[1]  6 NA 10
> subset(x, x > 5)
[1]  6 10

So when your data have some missing values, for example survey data, choose subset() or filtering method carefully. This tiny difference may cause unpredictable mistake which normally takes you a lot of time to debug the program.


Reference: The Art of R Programming by Norman Matloff