Update R in Ubuntu

If you installed R from the Ubuntu repository with

sudo apt-get install r-base

you most likely got an out of date version. In February 2018, that method still gave me R version 3.2.3 (2015-12-10). To get the latest versions of R and its packages, you need to add CRAN to the apt-get repositories. Do this with the code below. Enter one line at a time. Cut and paste to prevent errors.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/'
sudo apt-get update

Now if you open Synaptic Package Manager and search for r-base, you should see the installed versions and the latest versions of several packages (e.g.  r-base, r-base-core, r-base-core-dbg, r-base-dev, r-base.html). Mark the packages for upgrade and click on apply. This will most likely result in a major update of R so that the next time you run R only the base packages will be available. You will have to re-install any additional R packages that you use to match the updated version of R.

Adapted from https://www.digitalocean.com/community/tutorials/how-to-install-r-on-ubuntu-16-04-2

Get Execution Time for a Shell Script

If you need to know the execution time for a bash script, you can place it inside the script below. The the total run time will be printed to the screen after the script finishes.

res1=$(date +%s.%N)

<your script here>

res2=$(date +%s.%N)
dt=$(echo "$res2 - $res1" | bc)
dd=$(echo "$dt/86400" | bc)
dt2=$(echo "$dt-86400*$dd" | bc)
dh=$(echo "$dt2/3600" | bc)
dt3=$(echo "$dt2-3600*$dh" | bc)
dm=$(echo "$dt3/60" | bc)
ds=$(echo "$dt3-60*$dm" | bc)
printf "Total runtime: %d:%02d:%02d:%02.4f\n" $dd $dh $dm $ds

Shamelessly copied from a post on unix.stackexchange.com by jwchew on August 30, 2013 .