Logical vectors are coerced into numeric vecs in normal arithmetic
NA is not a value, only a marker for a quantity that is not available
Index vectors are of 4 types
Logical
Vector of I+
Vector of I−
Vector of char strings
Objects
# Empty veccharacter(0)numeric(0)# Intrinsic attrmode(x)length(x)attributes(x)# Conversionas.character()as.integer()# Changing lengthe = numeric()e[3] = 17 # Length = 3, first two are NAlength(e) = 2# Classx # Say it is of class data.frameunclass(x) # Prints like a matrix instead of a df
Intrinsic attributes
Mode
Length
Mode is basic type of fundamental constituents
Vectors (Atomic structure)
Are of the same mode (datatype)
Exception is NA
Empty vec can also have mode
Lists (Recursive structure)
Need not be of same mode
Class is a special attr all objects contain as reported by the function class
Factors
# Factorsstate <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa","qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas","sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa","sa", "act", "nsw", "vic", "vic", "act")a <- factor(state)a# [1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa# [16] tas sa nt wa vic qld nsw nsw wa sa act nsw vic vic act# Levels: act nsw nt qld sa tas vic walevels(a) # "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa"incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,59, 46, 58, 43)incmeans <- tapply(incomes, a, mean)# act nsw nt qld sa tas vic wa# 44.500 57.333 55.500 53.600 55.000 60.500 56.000 52.250
Factor is a vector object used to specify discrete grouping of components of other vec of same length
tapply applies a function to group of components
Arrays & Matrices
# Arraydim(z) = c(3, 5, 100) # z is a vectorx <- array(1:20, dim=c(4,5))# Indexinga[2]a[2, , ]# Matrix indices (Negatives not allowed)i <- array(c(1:3,3:1), dim=c(3,2))x[i] <- 0# Outer productab <- a %o% bab <- outer(a, b, "*") # Theta join
Array is a multiply subscripted collection of data entries
Vector can be used as arr if it has a dimension vector assigned to dim attribute
Values in data vector are column major
Mixed vector recycling
Expr scanned LTR
Short vec extended by repeating values
A vec operand longer than matrix/array generates error