2. Working with Vectors Flashcards

1
Q

Indexing vectors by position

A

Extract a single element:

vector[1]

Extract a range of elements:

vector[3:7]

Extract multiple elements:

vector[c(2,5,7)]

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

Displaying data types

A

Display the data type of a vector:

typeof(vector)

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

Naming vector elements

A

Assign name attributes to a vector:

names(vector) <- name_vector

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

Indexing vectors by name

A

Extract a single element:

vector[“name_2”]

Extract multiple elements:

vector[c(“name_1”, “name_2)]

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

Logical operators

A

Less than: vector_1 < vector_2

Greater than: vector_1 > vector_2

Less than or equal to: vector_1 <= vector_2

Greater than or equal to: vector_1 >= vector_2

Equal to: vector_1 == vector_2

Not equal to: vector_1 != vector_2

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

Logical indexing

A

Indexing into a numeric vector using a logical vector: numeric_vector[logical_vector]

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

Performing arithmetic on vectors

A

Add, divide, or multiply vectors:

vector_1 + vector_2

vector_1 / vector_2

vector_1 * vector_2

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

Concepts

A

R recognizes different data types:

  • Numeric (3, 5.66, 199, 6)
  • Character (“math”, “%”, “&”, “chem+math”)
  • Logical (TRUE, FALSE)

R is a 1-indexed programming language, which means that the first element in a vector is assigned a position of one.

When performing operations on vectors of unequal length, R “recycles” values of the shorter vector until the two vectors are the same length.

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