seq(), seq_len(), min(), max(), length(), range(),
any(), all()
Comparison operators:
<, <=, >, >=, ==, !=
Logical operators:
&& , ||, !, & , |, xor()
More on coercion:
is.logical(),is.integer(),is.double(),is.character()
as.logical(),as.integer(),as.double(),as.character()
Coercion often happens implicitly in function calls:
sum(rnorm(10) > 0)
Elements of a list can be any R object (including other lists)
Lists are created using list() :
car <- list("Ford", "Mustang", 1999L, TRUE); length(car)
is.list(car)
Can have nested lists:
# car, house, cat and sofa are other lists
house <- "Apartment";
cat <- list("Calico", "Flopsy", 3L);
sofa <- "Red"
possessions <- list(car, house, cat, sofa, "3000USD")
Or lists containing functions:
mean_list <- list(mean, "Calculates mean of input");
Elements of a list can be anything (including other lists)
Lists are vectors (but not "atomic vectors")
See: is.vector(), is.list(), is.atomic()
car
What does concatenating lists do? E.g. c(car, house)
What does concatenating a list with a vector do?
What does unlist()
do?
Just as with vectors, can apply typeof()
and class()
Another very useful function is str()
Provides a summary of the R object
str(car)
people <- c("Alice", "Bob", "Carol")
str(people)
Use brackets []
and double brackets [[]]
Brackets []
return a sublist of indexed elements
car[1]
typeof(car[1])
Double brackets [[]]
return element of list
car[[1]]
typeof(car[[1]])
Vector in double brackets recursively indexes list
possessions[[1]][[1]]
possessions[[c(1,1)]]
Can assign names to elements of a list
names(car) <- c("Manufacturer", "Make", "Year", "Gasoline")
car
Equivalently, on definition
car <- list("Manufacturer" = "Ford", "Make" = "Mustang",
"Year" = 1999, "Gasoline" = TRUE )
See also setNames()
car[c("Manufacturer", "Make")] # A two-element sublist
car[["Year"]] # A length-one vector
car$Year # Shorthand notation
car$year # R is case-sensitive!
The names() function can get/set names of elements of a list
names(car) # Returns a character vector
names(car)[4] <- "Gasoline"; names(car)
Names need not be unique or complete
Can remove names using unname()
Can also assign names to atomic vectors
names()
is an instance of an object attribute
These store useful information about the object
Get/set attributes using attributes()
attributes(car)
Get/set individual attributes using attr()
Other common attributes: class
, dim
and dimnames
Many have specific accessor functions e.g. class()
or dim()
You can create your own
Are two- and higher-dimensional collections of objects
These have an appropriate dim
attribute
my_mat <- 1:6 # vector
my_mat
dim(my_mat) <- c(2,3) # 2 rows and 3 columns
print(my_mat)
Equivalently
my_mat <- matrix(0 , nrow = 2, ncol=3) # ncol is redundant
my_mat
Arrays work similarly
my_arr <- array(1 : 8, c(2,2,2)); print(my_arr)
Useful functions include
typeof(), class(), str()
dim(), nrow(), ncol()
is.matrix(), as.matrix(), ...
dimnames(), rownames(), colnames()
dimnames(my_mat) <- list(c("r1", "r2"), c("c1", "c2", "c3"))
print(my_mat);my_mat['r1','c2']
A vector/list is NOT an 1-d matrix (no dim attribute)
is.matrix(1 : 6)
Use drop()
to eliminate empty dimensions
my_mat <- array(1 : 6, c(2,3,1)) # dim(my_mat) is (2,3,1)
print(my_mat)
my_mat <- drop(my_mat) # dim is now (2,3)
print(my_mat)
print(my_mat); my_mat[2,3] # Again, use square brackets
Excluding an index returns the entire dimension
my_mat[2,]
my_arr[1,,1] # slice along dim 2, with dims 1, 3 equal to 1
Usual ideas from indexing vectors still apply
print(my_mat[,c(2,3)])
We saw how to create a matrix from an array
my_mat <- (matrix(1 : 6, nrow = 3, ncol = 2)); print((my_mat)); print(t(my_mat))
In R matrices are stored in column-major order (like Fortran , and unlike C and Python )
print(my_mat[1,2])
Column-major order explains recycling to fill larger matrices
ones <- matrix(1, nrow = 3, ncol = 3); print(ones)
my_seq <- matrix(c(1,2,3), nrow = 3, ncol = 3); print((my_seq))
print(t(t(my_seq) + c(.1,.2,.3)))