Module 2 [R Programming] Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Which are the 5 basic (or ‘atomic’) classes of object in R?

A

The 5 basic classes are:

  • character
  • numeric (real numbers)
  • integer
  • complex
  • logical (TRUE/FALSE)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which is the most basic object in R?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which are the most common atributes of objects in R?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the c() function.

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if we create a vector with objects of mixed classes?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How we can do explicit coercion in R?

A

We can do explicit coercion in R using one of the following functions:

  • as.character()
  • as.numeric()
  • as.integer()
  • as.logical()
  • as.complex()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How a list of objects from different classes is printed in R console?

A

The elements will be printed as

[[list_index]]

[vector_1st_position] value

Example:

> x <- list(1, “a”)

> x

[[1]]

[1] 1

[[2]]

[2] “a”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which is the function (and its parameters) to create a matrix in R?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can we transform a vector into a matrix in R?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How we can combine to vectors into a matrix in R?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a factor in R and how they are created?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which operators can be used to extract subsets of R objects?

A

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 [[
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give examples of how to subset a vector using a single index, a range and logical operations.

A

See attached image.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly