Below are the solutions to this first set of exercises on the data.table package. library(data.table) ############### # # # Exercise 1 # # # ############### df <- fread(‘http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv’) df <- df[rep(1:nrow(df), 1000), ] format(object.size(df), ‘auto’) ## [1] "429.7 Mb" dim(df) ## [1] 4898000 12 df ## fixed acidity volatile acidity citric acid residual sugar ## […]

## Data table exercises: keys and subsetting

The data.table package is a popular R package that facilitates fast selections, aggregations and joins on large data sets. It is well-documented through several vignettes, and even has its own interactive course, offered by Datacamp. For those who want to build some mileage practising the use of data.table, there’s good news! In the coming weeks, […]

## Get-your-stuff-in-order exercises

In the exercises below we cover the basics of ordering vectors, matrices and data frames. We consider both column-wise and row-wise ordering, single and multiple variables, ascending and descending sorting, and sorting based on numeric, character and factor variables. Before proceeding, it might be helpful to look over the help pages for the sort, order, […]

## Get-your-stuff-in-order: solutions

Below are the solutions to these exercises on sorting and ordering. ############### # # # Exercise 1 # # # ############### x <- c(1, 3, 2, 5, 4) sort(x) ## [1] 1 2 3 4 5 sort(x, decreasing=T) ## [1] 5 4 3 2 1 ############### # # # Exercise 2 # # # ############### […]

## Bind exercises

Binding vectors, matrices and data frames using rbind and cbind is a common R task. However, when dimensions or classes differ between the objects passed to these functions, errors or unexpected results are common as well. Sounds familiar? Time to practice! Answers to the exercises are available here. Exercise 1 Try to create matrices from […]

## Bind exercises: solutions

Below are the solutions to these exercises on cbind and rbind. #################### # # # Exercise 1 # # # #################### a <- 1:5; b <- 1:5 m <- cbind(a, b) m ## a b ## [1,] 1 1 ## [2,] 2 2 ## [3,] 3 3 ## [4,] 4 4 ## [5,] 5 5 […]

## Practical uses of R object modes: some examples

One of our readers commented on our mode exercises post: “What real world tasks are you using mode to solve?” I think it’s an interesting question, from a somewhat larger perspective. Obviously, it’d be a waste of time to learn all kinds of obscure commands that don’t have a clear application in the real world. […]

## Mode exercises

In the exercises below we cover the basics of R object modes. Understanding mode is important, because mode is a very basic property of any R object. Practically, you’ll use the mode property often to convert e.g. a character vector to a numeric vector or vice versa. Before proceeding, first read section 3.1 of An […]

## Mode exercises: solutions

Below are the solutions to these exercises on the mode of R objects. # Exercise 1 mode(c(‘a’, ‘b’, ‘c’)) ## [1] "character" mode(3.32e16) ## [1] "numeric" mode(1/3) ## [1] "numeric" mode(sqrt(-2i)) ## [1] "complex" # Exercise 2 mode(pressure) ## [1] "list" mode(lm) ## [1] "function" mode(rivers) ## [1] "numeric" # Exercise 3 x <- list(LETTERS, […]