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 […]

## Factor exercises: solutions

Below are the solutions to these exercises on factors. # Exercise 1 x = c(1, 2, 3, 3, 5, 3, 2, 4, NA) levels(factor(x)) ## [1] "1" "2" "3" "4" "5" # (Answer: a) # Exercise 2 x <- c(11, 22, 47, 47, 11, 47, 11) factor(x, levels=c(11, 22, 47), ordered=TRUE) ## [1] 11 22 […]

## Index vectors solutions

Below are the solutions to these exercises on index vectors. # Exercise 1 x <- c("ww", "ee", "ff", "uu", "kk") x[c(2, 3)] ## [1] "ee" "ff" #(Answer: a) # Exercise 2 x <- c("ss", "aa", "ff", "kk", "bb") y <- x[c(2, 4, 4)] y[3] ## [1] "kk" # (Answer: c) # Exercise 3 x <- […]

## Index vectors

In the exercises below we cover the basics of index vectors. Before proceeding, first read section 2.7 of An Introduction to R, and the help pages for the sum, and which functions. Answers to the exercises are available here. Exercise 1 If x Are you a beginner (1 star), intermediate (2 stars) or advanced (3 […]

## Character vector exercises: solutions

Below are the solutions to these exercises. # Exercise 1 x <- "Good Morning! " nchar(x) ## [1] 14 # (Answer: c) # Exercise 2 x <- c ("Nature’s", "Best ") nchar(x) ## [1] 8 5 # (Answer: c) # Exercise 3 x <- c("Nature’s"," At its best ") nchar(x) ## [1] 8 15 # […]

## Character vector exercises

In the exercises below we cover the basics of character vectors. Before proceeding, first read section 2.6 of An Introduction to R, and the help pages for the nchar, substr and sub functions. Answers to the exercises are available here. Exercise 1 If x Are you a beginner (1 star), intermediate (2 stars) or advanced […]