Here I note down some tips and snippets for R that took me some time to figure out.

1. set multiple columns to be factors or numerics

This is straightforward: when I read data from file, I want to convert some columns into factors directly, or keep some columns as numeric:

data = read.csv("./path/to/file.csv", colClasses=c("userid"="factor", "account"="factor"))

colClasses could be used to indicate the class of each column by their names. Theres also a ``

2. plotting kit

  • include legend in the figure
theme(legend.justification=c(1,1), legend.position=c(1,1))

justification is the center point of the legend, and the posisiton is relative to that.

  • set axis tick text
scale_x_discrete(breaks=c("12", "13", "14"), 
	labels=c("Immediate","Short-term","Long-term"))

TBD

3. knitr config tags

  • save figure
{r figure\_name, 
	fig.width = 6, fig.height = 6, fig.path='results/figs/', dev='pdf'}

dev stands for device.