Monthly Archives: February 2009

3 posts

Highlighting R code in LaTeX using SweaveListingUtils

It is well known that you can use Sweave to integrate R code into a LaTeX document. However, how do we highlight R code in LaTeX? LaTeX got its own powerful package listings that can highlight source code from various languages. Moreover, the package SweaveListingUtils provides utilities for defining R as a listings “language”. To highlight your R code in your Sweave document you have to include the following code chunk:

Using an array for storing an object in R

Often you need fast access/lookup in R. For instance assume that you have the following data frame/matrix dat >head(dat) x y z [1,] 1 1 1 0.46 -0.89 0.88 -0.21 0.46 0.93 1.06 [2,] 1 1 2 0.72 -0.40 1.71 -0.52 0.95 NA NA [3,] 1 1 3 -0.49 -0.42 -0.26 -0.06 1.07 1.48 1.08 [4,] 1 1 4 -1.55 -0.90 0.15 -0.60 1.86 -1.15 NA [5,] 1 1 5 -0.46 -1.54 -0.40 NA NA NA NA [6,] 1 1 6 0.14 1.13 -2.42 0.86 0.13 -1.60 0.62 x, y and z represent an unique index and the rest of the columns represent a vector. The vectors may be of various length. To find the vector corresponding to index (x,y,z) you can do a normal search: