Chapter 1

Vectors

Is it true that this is a vector?

c(1)

Yes,

because c(1) is a collection of integer values of length one.


Is it true that this is a vector?

c(1,2,3)

Yes,

because c(1,2,3) is a collection of integer values of length three.


Is it true that this is a vector?

c("cat","dog","mouse")

Yes,

because c("cat","dog","mouse") is a collection of character values.


Is it true that this is a vector?

c(2.3,4.1)

Yes,

because c(2.3,4.1) is a collection of double values.


Is it true that this is a vector?

c(TRUE,TRUE,FALSE)

Yes,

because c(TRUE,TRUE,FALSE) is a collection of logical values.


Is it true that this is a vector?

list(1,"cat",4.1)

Yes,

because list(1,"cat",4.1) is a collection of values of type integer, character and double.


Is it true that this is an atomic vector?

list(1,"cat",4.1)

No,

because list(1,"cat",4.1) is a collection of values of type integer, character and double.


Is it true that this is an atomic vector?

c(1,2,3)

Yes,

because c(1,2,3) is a collection of values of type integer.


Is it true that this is an atomic vector?

c("cat","dog","mouse")

Yes,

because c("cat","dog","mouse") is a collection of values of type character.


Is it true that this is an atomic vector?

c(2.3,4.1)

Yes,

because c(2.3,4.1) is a collection of values of double character.


Is it true that this is a list?

list(1,"cat",4.1)

Yes,

because list(1,"cat",4.1) is a collection of values of type integer, character and double.


Is it true that this is a list?

c(1,"cat",4.1)

No,

because c(1,"cat",4.1) is a collection of values of type character.


Is it true that this is a list?

list(1,"cat",c(2,"dog"))

Yes,

because list(1,"cat",c(2,"dog")) is a collection of values of type integer, character and list.


Is it true that this is a vector?

c(one=1, two=2, three=3)

Yes,

because c(one=1, two=2, three=3) is a collection of named/tagged values.


Is it true that this is a vector?

c(one=1, one=2, one=3)

Yes,

because c(one=1, one=2, one=3) is a collection of named/tagged values.


Is it true that this is a vector?

list(one=1, one=2, one=c(1,2,"smile"))

Yes,

because list(one=1, one=2, one=c(1,2,"smile")) is a collection of named/tagged values.


What is names(w) where w is

list(one=1, two=2, three=c(1,2,"smile"))

"one" "two" "three"

because names(w) is a way of asking for the names of w.


What is w[1] where w is

c(1,2,3)

1

because 1 is the first element of atomic vector w.


What is w[1] where w is

list(1,2,"3")

list(1)

because 1 is a slice of the list w containing the first element.


What is w[[1]] where w is

list(1,2,"3")

1

because 1 is the first element of list w. More precisely, you can say that it's the first element in the slice of list w containing the first element of list w.


What is w[[2]] where w is

list(1,2,"3")

2

because 2 is the second element of list w. More precisely, you can say that it's the first element in the slice of list w containing the second element of list w.


What is w[1] where w is

c(1,2,"3")

"1"

because 1 is the first element of atomic vector w, and atomic vector w contains a value of type character.


What is w[2] where w is

c(1.0,2,3.0)

2.0

because 2 is the first element of atomic vector w, and atomic vector w contains a value of type double.


What is w[1] where w is

c(TRUE,2.0,3)

1.0

because TRUE is the first element of atomic vector w, and atomic vector w contains a value of type double.


What is w[[1]] where w is

list(TRUE,2.0,3)

TRUE

because TRUE is the first element in list w. More precisely, you can say that it's the first element in the slice of list w containing the first element of list w.


What is names(w[[2]]) where w is

list(one=1, two=2, three=c(1,2,"smile"))

"two"

because "two" is the name of the second element of list w.


What is w[c(1,3)] where w is

c("mercury","venus","earth","mars")

"mercury" "earth"

because "mercury" and "earth" are the first and third elements of vector w.


What is w[c(1,1,2,1)] where w is

c("mercury","venus","earth","mars")

"mercury" "mercury" "venus" "mercury"

because "mercury" and "venus" are the first and second elements of vector w.


What is w[4] where w is

c("mercury","venus",c("earth","mars"))

"mars"

because "mars" is the fourth element of atomic vector w.


What is w[4] where w is

list("mercury","venus",c("earth","mars"))

NULL

because the list w does not have four elements.


What is w[[3]][2] where w is

list("mercury","venus",c("earth","mars"))

"mars"

because c("earth","mars")) is the third element of list w and mars is the second element of atomic vector c("earth","mars")).


What is w[2:4] where w is

c("mercury","venus","earth","mars","jupiter","saturn")

"venus" "earth" "mars"

because "venus", "earth" and "mars" are the second through fourth elements of vector w.


What is w$planet_2 where w is

list("planet_1"="mercury","planet_2"="venus","planet_3"="earth")

"venus"

because planet_2 is the name of value "venus" in list w.


What is w$planet_2 where w is

c("planet_1"="mercury","planet_2"="venus","planet_3"="earth")

nothing

because you cannot use the $ syntax to refer to a named element of an atomic vector, only a named element of a list.