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

## Missing values: solutions

Below are the solutions to these exercises on missing values. # Exercise 1 # Answer: 6 # Exercise 2 # Answer: a # Exercise 3 # Answer: b # Exercise 4 # Answer: C # Exercise 5 W <- c (11, 3, 5, NA, 6) is.na(W) ## [1] FALSE FALSE FALSE TRUE FALSE # Exercise […]

## Missing values

Today we’re training how to handle missing values in a data set. Before starting the exercises, please first read section 2.5 of An Introduction to R. Solutions are available here. Exercise 1 If X Are you a beginner (1 star), intermediate (2 stars) or advanced (3 stars) R user?

## Matrix exercises: solutions

Below are the solutions to these matrix exercises. #1. x<-c(1,2,3) y<-c(4,5,6) z<-c(7,8,9) A<-cbind(x,y,z) rownames(A)<-c(“a”,”b”,”c”) ####if combined by rows A<-rbind(x,y,z) #2. is.matrix(A) ## [1] TRUE #if A is a data.frame then this should return false. So please note the #different usages between data.frame and matrix. #3. b<-c(1:12) B<-matrix(b, 4, 3, dimnames = list(c(“a”,”b”,”c”,”d”),c(“x”, “y”, “z”))) #4. […]

## Logical vectors and operators: solutions

Below are the solutions to these exercises on logical vectors and operators. # Solutions data <- mtcars # Q1 data[data$mpg > 15 & data$mpg < 20,] ## mpg cyl disp hp drat wt qsec vs am gear carb ## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 ## Valiant 18.1 […]