Module 2 [R Programming] Flashcards
Which are the 5 basic (or ‘atomic’) classes of object in R?
The 5 basic classes are:
- character
- numeric (real numbers)
- integer
- complex
- logical (TRUE/FALSE)
Which is the most basic object in R?
The most basic object in R is a vector, which:
- Can only have objects from the same class
- BUT the one exception is a list, which is represented as a vector but can contain objects of different classes (indeed, that’s usually why we use them)
- Empty vectors can be created using the vector() function
Which are the most common atributes of objects in R?
The most common atributes of objects in R:
- names and dimnames
- dimensions (e.g. matrices, arrays)
- class
- length
Attributes of an object can be accessed using the attributes() function.
Explain the c() function.
The c() function is used to create a vector of elements of the same class.
Examples:
- x <- c(“a”, “b”, “c”)
- x <- c(1:10)
- x <- c(1, 5, 10)
What happens if we create a vector with objects of mixed classes?
The vector will be created using the least common denominator (e.g. the less restrictive data type) for all objects.
Examples:
- x <- c(1.7, “a”) ## character vector
- x <- (TRUE, 2) ## numeric vector
- x <- (TRUE, “a”) ## character vector
How we can do explicit coercion in R?
We can do explicit coercion in R using one of the following functions:
- as.character()
- as.numeric()
- as.integer()
- as.logical()
- as.complex()
How a list of objects from different classes is printed in R console?
The elements will be printed as
[[list_index]]
[vector_1st_position] value
Example:
> x <- list(1, “a”)
> x
[[1]]
[1] 1
[[2]]
[2] “a”
Which is the function (and its parameters) to create a matrix in R?
To create a matrix in R we use the matrix(nrow = number, ncol = number) function. Another paramater that can be passed is the elements that will populate the matrix, like in this example:
> x <- matrix(1:6, nrow = 2, ncol = 3)
In this example, the elements are being set by columns. If we want elements being set by rows, we use byrow = TRUE paramater.
How can we transform a vector into a matrix in R?
We can transform a vector into a matrix in R using de dim() function. Example:
> x <- 1:10;
> dim(x) <- c(2, 5)
In this example, a vector of 10 positions is transformed in a matrix of 2 rows and 5 columns.
How we can combine to vectors into a matrix in R?
We can use 2 functions to do that:
- rbind(v1, v2): creates a matrix with the elements of v1 in the 1st row and the elements of v2 in the 2nd row
- cbind(v1, v2): creates a matrix with the elements of v1 in the 1st column and the elements of v2 in the 2nd column
What is a factor in R and how they are created?
A factor is a data type to represent categorical data, like male and female. Conceptually*, you can represent ordered and unordered factors.
Factors can be created as in the following example:
x <- factor(c(“male”, “female”, “female”), levels = c(“male”, “female”))
The parameter levels is passed when we desire a different base level (i.e. the 1st element) than the 1st in alphabetical order.
Factors are more descriptive than integers.
Factors are treated specially by lm() and glm() linear models functions.
* I didn’t see any specific sintax to represent one or another.
Which operators can be used to extract subsets of R objects?
The operators are:
- [ : extract a subset of elements (one or more) of the same class
- [[ : used to extract one element of a list or a data frame and the class of the returned object will not necessarilly be a list or a data frame
- $ : it is used to extract a subset of a list or data frame by name; semantics are similar to [[
Give examples of how to subset a vector using a single index, a range and logical operations.
See attached image.