Below are the solutions to these exercises on conditional execution. # Exercise 1 x <- -10 abs <- x if (x < 0) { abs = -x } cat("The absolute value of ", x, " is ", abs , "\n" ) ## The absolute value of -10 is 10 # Exercise 2 x <- 16 […]

## Scan exercises: solutions

Below are the solutions to these exercises on the scan function. # Exercise 1 v <- scan("http://r-exercises.com/wp-content/uploads/2015/12/scan01.txt") # Exercise 2 # a) vec <- scan("http://r-exercises.com/wp-content/uploads/2015/12/scan02.txt") # b) matrix <- matrix(scan("http://r-exercises.com/wp-content/uploads/2015/12/scan02.txt"), nrow=10) # Exercise 3 v <- scan("http://r-exercises.com/wp-content/uploads/2015/12/scan03.txt", what="character") # Exercise 4 mat <- matrix(scan("http://r-exercises.com/wp-content/uploads/2015/12/scan04.txt", sep="\t", nlines=5), ncol=2) df <- as.data.frame(mat) # Exercise 5 list <- […]

## Scan exercises

In the exercises below we cover the basics of the scan function. Before proceeding, first read section 7.2 of An Introduction to R. Answers to the exercises are available here. For each exercise we provide a data set that can be accessed through the link shown in the exercise. You can scan the data from […]

## Reading delimited data: solutions

Below are the solutions to these exercises on reading delimited data. # Exercise 1 df <- read.table("http://r-exercises.com/wp-content/uploads/2015/12/Table0.txt") df ## V1 V2 V3 V4 V5 ## 1 Alex 25 177 57 F ## 2 Lilly 31 163 69 F ## 3 Mark 23 190 83 M ## 4 Oliver 52 179 75 M ## 5 Martha […]

## Reading delimited data

In the exercises below we cover the basics of reading delimited data. Before proceeding, first read section 7.1 of An Introduction to R, and the help pages for the read.table function. Answers to the exercises are available here. For each exercise we provide a data set that can be accessed through the link shown in […]

## Data frame exercises

In the exercises below we cover the basics of data frames. Before proceeding, first read section 6.3.1 of An Introduction to R, and the help pages for the cbind, dim, str, order and cut functions. Answers to the exercises are available here. Exercise 1 Create the following data frame, afterwards invert Sex for all individuals. […]

## Data frame exercises: solutions

Below are the solutions to these exercises on data frames. # Exercise 1 Name <- c("Alex", "Lilly", "Mark", "Oliver", "Martha", "Lucas", "Caroline") Age <- c(25, 31, 23, 52, 76, 49, 26) Height <- c(177, 163, 190, 179, 163, 183, 164) Weight <- c(57, 69, 83, 75, 70, 83, 53) Sex <- as.factor(c("F", "F", "M", "M", […]

## List exercises

In the exercises below we cover the basics of lists. Before proceeding, first read section 6.1-6.2 of An Introduction to R, and the help pages for the sum, length, strsplit, and setdiff functions. Answers to the exercises are available here. Exercise 1 If: p Are you a beginner (1 star), intermediate (2 stars) or advanced […]

## List exercises: solutions

Below are the solutions to these exercises on lists. # Exercise 1 p <- c(2,7,8) q <- c("A", "B", "C") x <- list(p, q) x[2] ## [[1]] ## [1] "A" "B" "C" # (Answer: b) # Exercise 2 w <- c(2, 7, 8) v <- c("A", "B", "C") x <- list(w, v) x[[2]][1] <- "K" […]

## Factor exercises

In the exercises below we cover the basics of factors. Before proceeding, first read chapter 4 of An Introduction to R, and the help pages for the cut, and table functions. Answers to the exercises are available here. Exercise 1 If x = c(1, 2, 3, 3, 5, 3, 2, 4, NA), what are the […]