Chapter 2.1.5 Exercises
In [ ]:
x <- array(1:20, dim=c(4,5)) ; x # Generate a 4 by 5 array.
In [11]:
d <- outer(0:9, 0:9); d
In [12]:
fr <- table(outer(d, d, "-")); fr
length(fr)
dim(fr)
In [13]:
plot(fr, xlab="Determinant", ylab="Frequency")
In [17]:
stopifnot(t(xt[,,2]) == x[,,2],
t(xt[,,3]) == x[,,3],
t(xt[,,4]) == x[,,4])
UCB <- aperm(UCBAdmissions, c(2,1,3))
UCB[1,,]
summary(UCB) # UCB is still a continency table
In [16]:
x <- array(1:4, dim=c(2,2)) ; x
(j <- array(c(1:2,2:1), dim=c(2,2)))
as.vector(x)[as.vector(j)]
x[j]
In [37]:
# It’s also possible to have a column of a data frame that’s a matrix or array, as long as the number of rows matches the data frame. (This requires a slight extension to our definition of a data frame: it’s not the length() of each column that must be equal; but the NROW().) Like with list-columns, you must either add after creation, or wrap in I().
dfm <- data.frame(
x = 1:3 * 10
)
dfm$y <- matrix(1:9, nrow = 3)
dfm$z <- data.frame(a = 3:1, b = letters[1:3], stringsAsFactors = FALSE)
str(dfm)
In [46]:
# Data frames allow you to label each row with a “name”, a character vector containing only unique values:
df3 <- data.frame(
age = c(35, 27, 18),
hair = c("blond", "brown", "black"),
row.names = c("Bob", "Susan", "Sam")
)
df3
In [106]:
v <- data.frame("SN" = 1:2, "Age" = c(21,15), "Name" = c("John", "Dora")); v
class(v)
# In this example, x can be considered as a list of 3 components with each component having a two element vector.
In [16]:
str(v) # structure of v
In [29]:
trees
In [36]:
# A data frame can be examined using functions like str() and head().
str(trees)
head(trees,n=10)
In [42]:
trees[trees$Height > 82,] # selects rows with Height greater than 82
In [133]:
v <- rbind(v,list(1,16,"Paul")); v
In [132]:
v <- cbind(v,Age=c("21","15", "16")); v
In [1]:
x <- data.frame(foo = 1:4, bar = c(T, T, F, F))
x
nrow(x)
ncol(x)
In [2]:
# Exam data
exam <- data.frame(
id = 1:5,
q1 = c(1, 5, 2, 3, 2),
q2 = c(8, 10, 9, 8, 7),
q3 = c(3, 7, 4, 6, 4))
exam
In [4]:
# Demographic data
demographics <- data.frame(
id = 1:5,
sex = c("f", "m", "f", "f", "m"),
age = c(25, 22, 24, 19, 23))
demographics
In [5]:
# Combine exam and demographics
combined <- merge(x = exam,
y = demographics,
by = "id")
combined
In [6]:
# Mean q1 score for each sex
aggregate(formula = q1 ~ sex,
data = combined,
FUN = mean)
aggregate
In [9]:
# Many summary statistics by sex using dplyr!
library(dplyr)
combined %>% group_by(sex) %>%
summarise(
q1.mean = mean(q1),
q2.mean = mean(q2),
q3.mean = mean(q3),
age.mean = mean(age),
N = n())
In [9]:
# Many summary statistics by sex using dplyr!
library(dplyr)
combined %>% group_by(sex) %>%
summarise(
q1.mean = mean(q1),
q2.mean = mean(q2),
q3.mean = mean(q3),
age.mean = mean(age),
N = n())
In [3]:
x <- factor(c("ab", "cd", "ab"), levels = c("ab", "cd", "ef")); x
typeof(x)
attributes(x)
In [17]:
z <- 0:9; z
digits <- as.character(z); digits
d <- as.integer(digits); d
# # Now d and z are the same.12 There is a large collection of functions of the form as.something() for either coercion from one mode to another, or for investing an object with some other attribute it may not already possess. The reader should consult the different help files to become familiar with them.
In [49]:
r <- factor(c("a","b")); r
levels(r) <- list(C = "C", A = "a", B = "b"); r
In [56]:
factor(c(1, 2, 3), labels=c('a', 'b', 'c'))
factor(c(3.2, 10, 500000), labels=c('a', 'b', 'c'))
factor(c(.49, 1, 5), labels=c('a', 'b', 'c'))
In [13]:
x = sample(state.name, 10000, replace=T)
format(object.size(x), units='Kb')
# Because of the integer+metadata representation, factors are actually smaller than character strings, often notably so.
In [6]:
# While atomic vectors are most commonly turned into matrices, the dimension attribute can also be set on lists to
# make list-matrices or list-arrays:
l <- list(1:3, "a", TRUE, 1.0)
dim(l) <- c(2, 2)
l
l[[1, 1]]
# These are relatively esoteric data structures, but can be useful if you want to arrange objects into a grid-like
# structure.
# For example, if you’re running models on a spatio-temporal grid, it might be natural to preserve the grid
# structure by storing the models in a 3d array.
In [13]:
# Unlike atomic vectors, list() can contain a mix of objects.
# Following is an example of a list having three components each of different data type. In this example, a, b and
# c are called tags which makes it easier to reference the components of the list.
x <- list("a" = 2.5, "b" = TRUE, "c" = 1:3); x
typeof(x)
length(x)
In [1]:
v <- list("John", 19, c("English", "French")); names(v) <- c("name", "age", "speaks"); names(v[[3]]) <- c("english", "french"); v
In [ ]:
In [20]:
# Lists can be accessed in similar fashion to vectors. Integer, logical or character vectors can be used for
# indexing.
v[c(1:2)] # index using integer vector
v[-2] # using negative integer to exclude second component
v[c(T,F,F)] # index using logical vector
v[c("age","speaks")] # index using character vector
In [21]:
# Indexing with [ as shown above will give us sublist not the content inside the component.
# To retrieve the content, we need to use [[. However, this approach will allow us to access only a single component at a time.
v["age"]
typeof(v["age"])
v[["age"]]
typeof(v[["age"]])
v[[2]]
In [22]:
# An alternative to [[, which is used often while accessing content of a list is the $ operator. They are both the same except that $ can do partial matching on tags.
v$name # same as v[["name"]]
v$a # partial matching, same as v$ag or v$age
v[["a"]] # cannot do partial match with [[
In [29]:
# We can delete a component by assigning NULL to it.
v[["age"]] <- NULL; str(v)
In [46]:
# Matrices are vectors with a dimension attribute.
m <- matrix(nrow = 2, ncol = 3); m
dim(m)
attributes(m)
In [10]:
# It is also possible to change names
colnames(x) <- c("C1","C2","C3")
rownames(x) <- c("R1","R2","R3")
x
In [29]:
j <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"), c("A","B","C"))); j
In [30]:
j[,"A"]
j[TRUE,c("A","C")]
j[2:3,c("A","C")]
In [34]:
j[j<5] <- 0; j # modify elements less than 5
In [44]:
(m1 <- matrix(1:20, 4, 5))
upper.tri(m1)