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

## Array exercises

Exercise 1 Create an array (3 dimensional) of 24 elements using the dim() function. Exercise 2 Create an array (3 dimensional) of 24 elements using the array() function. Exercise 3 Assign some dimnames of your choice to the array using the dimnames() function. Exercise 4 Assign some dimnames of your choice to the array using […]

## Matrix exercises

Please note, solutions are available here. Exercise 1 Create three vectors x,y,z with integers and each vector has 3 elements. Combine the three vectors to become a 3×3 matrix A where each column represents a vector. Change the row names to a,b,c. Think: How about each row represents a vector, can you modify your code […]

## Logical vectors and operators

Before you start, enter the following code: data How difficult was this for you? (1 star=easy, 10 stars=hard) Are you a beginner (1 star), intermediate (2 stars) or advanced (3 stars) R user?

## Regular sequences: solutions

Below are the solutions to these regular sequence exercises. # Excercise 1 # Using the seq() function generate the sequence 2,5,8,11 seq(from = 2,to = 13,by = 3) ## [1] 2 5 8 11 # Exercise 2: # Use the seq() function to generate the sequence 9,18,27,36,45 seq(from = 9, to = 45, by = […]