R Language Notes

First published: 13 Nov 2016
Last updated: 13 Nov 2016

Installing R and RStudio

The easiest and quickest way to install R on Debian is to download and install the latest RStudio package as follows.

Copy the link to the latest version of RStudio from https://www.rstudio.com/products/rstudio/download/.

For example, this will look like https://download1.rstudio.org/rstudio-0.99.878-amd64.deb.

Download it using wget, for example, wget https://download1.rstudio.org/rstudio-0.99.878-amd64.deb. Finally, install it using dpkg as follows: sudo dpkg -i rstudio-0.99.878-amd64.deb

Installing a package

Use install.packages("package name")

Loading a package

Use library(package name)

Viewing data

Use View(variable)

Some basic dplyr commands

Suppose that for this example we are using a data frame with 2 columns, country and age, for instance. To keep the data for say country Italy, we use italy_only = filter(data, country=="Italy"). This filters out all the other data except for the ones where country is equal to Italy.

To select only one column, say age, use select(italy_only, age).

It is useful to combine commands on one line by using pipes as follows:

Italy_age_only = filter(data, country=="Italy") %>% select(age) %>% unlist

Convert from a data frame to a vector

Use unlist(dataframe)

Check proportion of samples falling within range

Use mean(averages <= high) - mean(averages < low), where averages is an array of values, and low and high are the respective limits.