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

# Solutions

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

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

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

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

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

## Vector exercises: solutions

Below are the solutions to these vector exercises. Exercise 1 answer e Exercise 2 answer e Exercise 3 answer c Exercise 4 answer b Exercise 5 answer d Exercise 6 answer c Exercise 7 answer c Exercise 8 answer c Please help us to improve R-exercises: Did you […]