Ready, set, go! On R-exercises, you will find hundreds of exercises that will help you to learn R. We’ve bundled them into exercise sets, where each set covers a specific concept or function. An exercise set typically contains about 10 exercises, progressing from easy to somewhat more difficult. In order to give you a full […]

## As.Date() Exercises

The as.date() function creates objects of the class “Date“, via input of character representations of dates. Answers to the exercises are available here. Exercise 1 The format of as.Date(x, …) accepts character dates in the format, “YYYY-MM-DD”. For the first exercise, use the c() function, and as.date(), to convert “2010-05-01” and “2004-03-15” to class “date” […]

## Data Shape Transformation With Reshape()

reshape() is an R function that accesses “observations” in grouped dataset columns and “records” in dataset rows, in order to programmatically transform the dataset shape into “long” or “wide” format. Required dataframe: data1 <- data.frame(id=c("ID.1", "ID.2", "ID.3"), sample1=c(5.01, 79.40, 80.37), sample2=c(5.12, 81.42, 83.12), sample3=c(8.62, 81.29, 85.92)) Answers to the exercises are available here. Exercise 1 […]

## Summary Statistics With Aggregate()

The aggregate() function subsets dataframes, and time series data, then computes summary statistics. The structure of the aggregate() function is aggregate(x, by, FUN). Answers to the exercises are available here. Exercise 1 Aggregate the “airquality” data by “airquality$Month“, returning means on each of the numeric variables. Also, remove “NA” values. Exercise 2 Aggregate the “airquality” […]

## Scripting Loops In R

An R programmer can determine the order of processing of commands, via use of the control statements; repeat{}, while(), for(), break, and next Answers to the exercises are available here. Exercise 1 The repeat{} loop processes a block of code until the condition specified by the break statement, (that is mandatory within the repeat{} loop), […]

## zoo time series exercises

The zoo package consists of the methods for totally ordered indexed observations. It aims at performing calculations containing irregular time series of numeric vectors, matrices & factors. The zoo package interfaces to all other time series packages on CRAN. This makes it easy to pass the time series objects between zoo & other time series […]

## Lattice exercises – part 2

In this set of exercises we will use lattice package. Firstly, we have to install this package with command install.packages(“lattice”) and then we will call it library(lattice) . Lattice package permits us to create univariate, bivariate and trivariate plots. For this set of exercises we will see trivariate plots. We will use a dataset example […]

## Accessing Dataframe Objects Exercises

The attach() function alters the R environment search path by making dataframe variables into global variables. If incorrectly scripted, the attach() function might create symantic errors. To prevent this possibility, detach() is needed to reset the dataframe objects in the search path. The transform() function allows for transformation of dataframe objects. The within() function creates […]

## Cross Tabulation with Xtabs exercises

The xtabs() function creates contingency tables in frequency-weighted format. Use xtabs() when you want to numerically study the distribution of one categorical variable, or the relationship between two categorical variables. Categorical variables are also called “factor” variables in R. Using a formula interface, xtabs() can create a contingency table, (also a “sparse matrix”), from cross-classifying […]

## Lattice exercises – part 1

In the exercises below we will use the lattice package. First, we have to install this package with install.packages(“lattice”) and then we will call it library(lattice) . The Lattice package permits us to create univariate, bivariate and trivariate plots. For this set of exercises we will see univariate and bivariate plots. We will use a […]