Chapter 4

Factors and Arrays

Is it true that this is a factor?

factor(c(1,2,3,4,5,6,7,8))

Yes

because it is a vector of eight values passed as an argument to the function factor.


Is it true that this is a factor?

factor(1,2,3,4,5,6,7,8)

No

because it is eight values passed as eight arguments to the function factor.


How many levels are in factor f where

f <- factor(c(1,1,2,1,3,4,3,5,1))

Five

because there are five unique values in factor f.


What is levels(f) where

f <- factor(c(1,1,2,1,3,4,3,5,1))

[1] "1" "2" "3" "4" "5"

because levels() gets a list of factors.


What is levels(f) where

f <- factor(c(1,3,4,4), levels=c(1,2,3,4))

[1] "1" "2" "3" "4"

because a factor can have levels not represented in the data.


What is f where

f <- factor(c(1,3,4,4,5), levels=c(1,2,3,4))

[1] 1 3 4 4 <NA>

because a factor cannot have data not represented in the levels.


What is f where

f <- factor(c("sunny", "cloudy", "rainy", "cloudy"), levels=c("sunny", "cloudy", "windy", "rainy"))

[1] 1 3 4 4 <NA>

because a factor cannot have data not represented in the levels.


How many levels does f have where f is

f <- factor(1:6)

six

because 1:6 produces a vector of values 1 through 6.


How many levels does f have where f is

f <- factor(1:6,1:9)

nine

because the second argument to factor() is levels and 1:9 produces a vector of values 1 through 9.


Is it true that this is an array?

array(1:8, dim=c(8))

yes

because it is a collection of values with one dimension.


Is it true that this is an array?

array(1:8, dim=c(2,4))

yes

because it is a collection of values with two dimensions.


What is class(a) where

a <- array(1:8, dim=c(2,4))

matrix

because R treats two-dimensional arrays and matrices the same, and uses matrix to refer to both.


Is it true that this is an array?

array(1:8, dim=c(2,2,2))

yes

because it is a collection of values with three dimensions.


Is it true that this is an array?

array(1:3, dim=c(2,4,2))

yes

because the data used will be recycled to fit the dimensions.


Is it true that this is an array?

array(1:6, dim=c(2,2,2), dimnames=c("rows","columns","groups")

no

because dimnames must be a list.


Is it true that this is an array?

array(1:6, dim=c(2,2,2), dimnames=list("rows","columns","groups")

no

because dimnames does not have a value for each component of each dimension.


Is it true that this is an array?

array(1:6, dim=c(2,2,2), dimnames=list(c("row1","row2"),c("col1","col2"),c("group1","group2")))

yes

because dimnames has a value for each component of each dimension.