News

Add comments on your R objects

It is always good to add comments on your R code. But it is also good to add comments on your objects like variables, functions, etc.

> Mydata comment(Mydata) <- "This is a data from a sequence" > Mydata
 [1] 1 2 3 4 5 6 7 8 9 10
 > comment(Mydata)
 [1] "This is a data from a sequence"
 > str(Mydata)
 atomic [1:10] 1 2 3 4 5 6 7 8 9 10
 - attr(*, "comment")= chr "This is a data from a sequence"

Set fonts size in R plot

You probably have discovered that R usually gives you different size of fonts in different figures. That is because a numerical value (cex in par function) giving the amount by which plotting text and symbols is magnified relative to a default value. This starts as ‘1’ when a device is opened, and is reset when the layout is changed, e.g. by setting ‘mfrow’.

But the final fonts size for the main title (or axis, lab, sub) is determined by the product of the three variables, ps, cex, and cex.main (or cex.axis, cex.lab, cex.sub), respectively.

So if you want 12 points fonts in the title, you may set the following options

par(ps = 12, cex = 1, cex.main = 1)

A fast routine of making a commutation matrix

R does not have this function by default, so I wrote a faster routine K() to create a commutation matrix.We always use the notation of commutation matrix on the paper. But when we put it into practice, we typically need the result of a dense matrix by pre- or post-multiplied by a commutation matrix. i.e., KX or YK., where K is the commutation matrix and X, Y are dense matrices. This could be a disaster when the dimensions are big in the usual way of multiplication. And I found a very easy and efficient way of doing this.

Please visit the git page for the updated code