# R: ISO codes and country names

install.packages(‘maps’) iso = maps::iso3166 > str(iso) 'data.frame': 269 obs. of 5 variables: \$ a2 : chr "AW" ... \$ a3 : chr "ABW" ... \$ ISOname : chr "Aruba" .. \$ mapname : chr "Aruba" ... \$ sovereignty: chr "Netherlands" ...

# How long did my R script run?

This to the beginning: strt<-Sys.time() And this to the end: print(Sys.time()-strt) Example output: print(Sys.time()-strt) Time difference of 16.39691 secs

# 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):

# Converting R Markdown to Latex

Install Pandoc: http://pandoc.org/ library(knitr) knit('report.Rmd') #This creates 'report.md' Open the Terminal, Command Prompt (search for cmd) or Windows Powershell, go to the folder and do: pandoc -s report.md -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.

# Why does a linear model without an intercept (forced through the origin) have a higher R-squared value? [calculated by R]

This is a short note based on this. Answer in short: Because different formulas are used to calculate the R-squared of a linear regression, depending on whether it has an intercept or not. R2 for a linear model that has an intercept: , where y is the variable that the linear model is trying to predict (the response variable), y^ is the predicted value and y- is the mean value of the response variable.
• page 2 of 2

#### Riinu Ots

if it aint broke, you’re outdated

Senior Data Manager

Edinburgh, UK