Default Paper reading


This week’s  discussion paper for the PhD Seminar Series in Statistics is

The reading guide

In order to have more fun in the reading, we are aiming to complete  the two tasks before Sunday, Dec 22 .

  • Post at least one question here you found during the reading.
  • Give comments to at least one question that other people asked.

Happy reading!

Default R

Source a bunch of files under given folders and/or subfolders

I wrote a function sourceDir() that can source all the functions under given folders and/or subfolders. This function is very much like “Set Path” in Matlab.Please visit this page to obtain the update to dated version.

To use it more efficiently, I suggest you put the function at some place, e.g. ~/workspace/R_utils/sourceDir.R, and write the following lines in .Rprofile file.

.my.env <- new.env()

Next time the function will be automatically loaded under a personal environment .my.env when R is launched and you can use it directly. The advantage of this is that rm(list=ls()) command will not remove the function sourceDir. But if you really want to remove sourceDir function, you can use rm(list=ls(all=TRUE)) command instead.

Default R

How to prevent dropping dimensions in a matrix/array?

When you create a matrix in the usual way like this,

> a <- matrix(rnorm(10),2,5)
> a
       [,1]      [,2]       [,3]       [,4]       [,5]
 [1,]  1.3488918 0.6225795 -0.7444514  1.3130491  1.7877849
 [2,] -0.2385392 0.5656759  0.9037435 -0.2217444 -0.2656875

the dimension dropped after picking up a single row or column in this way,

> b <- a[,1]
> b
 [1]  1.3488918 -0.2385392
 > dim(b)

The solution is to try it with a parameter drop = FALSE,

> b <- a[,1,drop = FALSE]
 > b
 [1,]  1.3488918
 [2,] -0.2385392
 > dim(b)
 [1] 2 1