Riinu's scripting diary

If it ain't broke, you're outdated

Adding a Cron job using the ed editor

These commands (after the first one) work in the ed editor. ed editor is used in the RStudio server shell.

sudo crontab -e                                     #opens crontab file in chosen editor

a                                                   #add to file
0,30 * * * * Rscript /home/user/folder/script.R     #command to add
.                                                   #finished editing
,p                                                  #print file content to check
w                                                   #save changes
Q                                                   #quit

In this example the script will run every 0 hours, 30 minutes, see Ubuntu: How do I set up a CRON job for other options.


Add abcd labels on ggplot facets


How long did my R script run?

This to the beginning:


And this to the end:


Example output:

> print(Sys.time()-strt)
Time difference of 16.39691 secs


Adding space between rows in LaTex tables

By default, LaTex tables are very tight:


\caption{My caption}
Rows  & Column 1 & Column 2 \\ \midrule
Row 1 & 1234     & 2345     \\
Row 2 & 3456     & 4567     \\
Row 3 & 5678     & 6789     \\
Row 4 & 7890     & 8901     \\
Row 5 & 9012     & 10000    \\ \bottomrule

Screen Shot 2016-04-11 at 11.28.54

Adding this to the document preamble will add space between the rows:


Screen Shot 2016-04-11 at 11.24.26

And this command can be used to add space between rows manually:


Screen Shot 2016-04-11 at 11.25.38

My minimal LaTex preamble

My minimal example:


\usepackage{graphicx} %gives the \includegraphics[width=0.5\textwidth]{my_image}

\usepackage{fullpage} %gets rids of the wide default borders
\renewcommand{\baselinestretch}{1.5} %space between lines


Hello hello hello


And then one that is not so minimal, but still pretty basic and useful:



\usepackage{graphicx} %gives the \includegraphics[width=0.5\textwidth]{my_image}
\usepackage{booktabs} %for nicer tables
\usepackage{tabu} %advanced control over tables

\renewcommand{\thetable}{S\arabic{table}} %if this is supplement (this numbers figures as S1, S2...), comment out if main
\renewcommand{\thefigure}{S\arabic{figure}} %if this is supplement, replace S with A if Appendix

\usepackage{amsmath} % amsmath provides extra maths symbols
\newcommand{\degree}{\ensuremath{^\circ}} %for some reason I can't find a degree symbol from other packages or the packages I do find it from clash with some others
\usepackage{times} %these packages will make texttildelow look normal

\usepackage{natbib} %references as citet (textual) or citep (parenthetical)

\usepackage{fullpage} %gets rids of the wide default borders
\captionsetup[table]{skip=10pt} %this adds space between the table caption and the table itself
\renewcommand{\baselinestretch}{1.5} %space between lines


Hello hello hello



(Syntax highlighting with

How to “increase” array resolution in R (replicate each element both column-wise and row-wise)

One picture says more than a thousand words. You have what is one the left, and you want what is on the right.


my_matrix = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow=3)
#matrix is a 2D array, this next row creates a third dimension,
#duplicating the data
my_array = array(my_matrix, dim = c(3,3,2))

There are a few different ways to do this, but by far the cleanest and quickest way is to just select the rows and columns multiple times, by replicating row and column numbers (instead of actually replicating each element):

increased_matrix = my_matrix[rep(1:nrow(my_matrix), each=3), rep(1:ncol(my_matrix, each=3)]
#3D (same really, just one extra comma for the third dimension):
increased_array = my_array[rep(1:nrow(my_array), each=3), rep(1:ncol(my_array, each=3), ]

Note that by default, in rep(something, n) the n is times so equivalent to rep(something, times=n), but in this case we need to use each instead of times.


Cut a time period from netCDF with nco

ncks -d time,start_time,end_time

start_time and end_time are integers.

Add -F, if you want to use Fortran indexing (to start from 1).

No space between dimension name and start-end points!

Converting R Markdown to Latex

Install Pandoc:

knit('report.Rmd') #This creates ''


Open the Terminal, Command Prompt (search for cmd) or Windows Powershell, go to the folder and do:

pandoc -s -o report.tex

And that’s it!

(Read this, if you want vector images.)

Reordering factor levels in R and what could go wrong

I’ve recently started using ggplot2 in addition to lattice (see this post that I made a while ago, explaining how I got into using lattice in the first place). Hint: when using ggplot2, you’ll need to use of the reshape2 package (also written by the amazing Hadley Wickham) to get your data into a form that ggplot2 works best with. Another thing that you’ll want to think about when using ggplo2 is factor levels. This post will show how to (and also how not to) rearrange factor levels in R.

Let’s create a quick barplot with strings as x labels.

#create dummy data
a = paste('my', 1:11)
b = 1:11
df = data.frame(a, b)
qplot(a, b, data=df, geom='bar', stat='identity') + 
  theme(axis.text=element_text(size=16, angle=45))



As df$a is an array of strings, R sets the factor levels alphabetically: my 1, my 10, my 11, my 2…which is not what we want, so let’s rearrange factor levels:

df$a = factor(df$a, levels = paste('my', 1:11))
qplot(a, b, data=df, geom='bar', stat='identity') + 
  theme(axis.text=element_text(size=16, angle=45))



And finally, the wrong way to rearrange factor levels would be by using the levels() function:

df = data.frame(a, b)
levels(df$a) = paste('my', 1:11)
qplot(a, b, data=df, geom='bar', stat='identity') + 
  theme(axis.text=element_text(size=16, angle=45))



So be careful – if your data is not as obvious as this example and you are a bit new to factors and levels, you might end up plotting wrong results (like on the last example, “my 2” and “my 3” were plotted with the values 10 and 11).

Powered by

Up ↑