Instruction

First read Appendix B of the textbook Modern Data Science with R: http://mdsr-book.github.io.

The following exercises are taken from Appendix B.

1. Typing commands

A user has typed the following commands into the console.

obj1 <- 2:10
obj2 <- c(2, 5)
obj3 <- c(TRUE, FALSE)
obj4 <- 42

What values are returned by the following commands?

obj1 * 10
obj1[2:4]
obj1[-3]
obj1 + obj2
obj1 * obj3
obj1 + obj4
obj2 + obj3
sum(obj2)
sum(obj3)

SOLUTION:

2. Typing more commands

A user has typed the following commands into the console.

a <- c(10, 15)
b <- c(TRUE, FALSE)
c <- c("happy", "sad")

What do each of the following commands return? Describe the class of the object as well as its value.

data.frame(a, b, c)
cbind(a, b)
rbind(a, b)
cbind(a, b, c)
list(a, b, c)[[2]]

SOLUTION:

3. Typing even more commands

A user has typed the following commands into the console.

mylist <- list(x1="sally", x2=42, x3=FALSE, x4=1:5)

What values do each of the following commands return?

is.list(mylist)
names(mylist)
length(mylist)
mylist[[2]]
mylist[["x1"]]
mylist$x2
length(mylist[["x4"]])
class(mylist)
typeof(mylist)
class(mylist[[4]])
typeof(mylist[[3]])

SOLUTION:

4. Concepts

The following concepts should have some meaning to you: package, function, command, argument, assignment, object, object name, data frame, named argument, quoted character string. Construct an example of R commands that make use of at least four of these. Label which part of your example R command corresponds to each.

SOLUTION:

5. Names

Which of these kinds of names should be wrapped with quotation marks when used in R? 1. function name 2. file name 3. the name of an argument in a named argument 4. object name

SOLUTION:

6. What’s wrong?

What’s wrong with this statement?

help(NHANES, package <- "NHANES")

7. CPS

Consult the documentation for CPS85 in the mosaicData package to determine the meaning of CPS.

SOLUTION:

library(mosaicData)
# solution does here

8. Errors

For each of the following assignment statements, describe the error (or note why it does not generate an error).

result1 <- sqrt 10
result2 <-- "Hello to you!"
3result <- "Hello to you"
result4 <- "Hello to you
result5 <- date()

SOLUTION: