Post tagged: r

Long and wide matrices

A quick lesson on representing data and reshaping matrices

There are two broad ways data can be stored in a matrix. In "wide" format, everything for a given subject / record is on a single line / row. For example:

patientId   control_cells   control_temp   drugA_cells   drugA_temp   drugB_cells   drugB_temp
1           ...
2           ...

Whereas the "long" format has multiple rows for a subject, one for ...

"Indexing and slicing"

Coming from other (arguably saner) programming languages, R's indexing behaviour kept catching me off guard. When should I use [] and when [[]]? What's the difference between accessing a member by name or using that name as an index? Why is iterating over a list so fiddly? Here's an ...

Primitive R objects and S3

Objects are just lists and class is just an attribute.

Primordial R objects

The first thing to know is that nearly every object in R is really just a list with named elements. For example, the results returned from "summary" are returned in an object:

x <- summary(c(1:10))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    1 ...

An introduction to objects in R

The various types of OOP in R, whatever that is.


Doing object-oriented programming (OOP) in R can be complicated. This is because when you look in R for the usual idioms and features of OOP:

  • some are missing
  • some are present but implemented in unusual albeit valid ways
  • some ...

Reference classes

Also sometimes wittily referred to as S5 or R5.

A more recent development in R is Reference classes. These promise, at last, fully blown objects and classes like those of C++ and Java. Naturally, this innovation comes with some downsides:

  • Reference classes are still, as yet, lightly documented
  • They use their own form of syntax and idioms
  • The C ...

Writing knitr in restructured text

Swapping out Markdown for a different markup.

knitr is a useful R package/tool for documenting analysis. Basically, it allows the embedding of R code "chunks" within a simple text document. This document can then be "knitted", which means that the R code is interpreted and reinserted in the document along with the results of that code ...