Chapter21 : Vectors Flashcards

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

What are the two types of Vectors?

A

a. Atomicvectors, of which there are six types:logical,integer,double,character,complex, andraw. Integer and double vectors are collectively known asnumericvectors.
b. Lists, which are sometimes called recursive vectors because lists can contain other lists

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

What is diff btweem atomic vectors and list vectors?

A

The chief difference between atomic vectors and lists is that atomic vectors arehomogeneous, while lists can beheterogeneous

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

What are two key properties every vector has?

A

Itstype, which you can determine withtypeof().

Itslength, which you can determine withlength()

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

What are Numeric vector?

A

Numeric

Integre and double are all numeric
In R, numbers are doubles by default. To make an integer, place anLafter the number: 1.5L

1) Integers have one special value:NA, while doubles have four:NA,NaN,Infand-Inf. All three special valuesNaN,Infand-Infcan arise during division:
a) c(-1, 0, 1) / 0
#> [1] -Inf NaN Inf

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

What are character vector?

A

Character vectors are the most complex type of atomic vector, because each element of a character vector is a string, and a string can contain an arbitrary amount of data

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

What is Coercion?

A

There are two ways to convert, or coerce, one type of vector to another:

1) Explicit coercion happens when you call a function likeas.logical(),as.integer(),as.double(), oras.character()

Implicit coercion happens when you use a vector in a specific context that expects a certain type of vector. For example, when you use a logical vector with a numeric summary function, or when you use a double vector where an integer vector is expected.

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

What are test functions in R?

A

Sometimes you want to do different things based on the type of vector. One option is to usetypeof(). Another is to use a test function which returns aTRUEorFALSE. Base R provides many functions likeis.vector()andis.atomic(), but they often returns surprising results. Instead, it’s safer to use theis_*functions provided by purrr, which are summarised in the table below. is_logical() , is_integer() etc

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

Scalars and recycling rules

A

As well as implicitly coercing the types of vectors to be compatible, R will also implicitly coerce the length of vectors. This is called vectorrecycling, because the shorter vector is repeated, or recycled, to the same length as the longer vector.

This is generally most useful when you are mixing vectors and “scalars”. I put scalars in quotes because R doesn’t actually have scalars: instead, a single number is a vector of length 1. Because there are no scalars, most built-in functions arevectorised, meaning that they will operate on a vector of numbers. That’s why, for example, this code works

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

How to named vectors

A

Naming vectors

1) All vectors can be name.and can be name using creation with c() e.g c( x=1 , y=3 , p=5)

2) Or after the fact withpurrr::set_names()
a) set_names(1:3, c(“a”, “b”, “c”))

Named vectors are most useful for subsetting, described next.

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

Vector subsetting. Wat are Four ways to subset a vector?

A

a) Using numeric vector containing only integer

Subsetting with logical vector keeps all values corresponding to TRUE value

If u have a named vector , u can subset it with character vector

The simplest type of subsetting is nothing,x[], which returns the completex

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

How to use numeric vector containing only integer to access vector?

A

x [1] “three” “two” “five”

Negative values drop the elements at the specified positions:
x[c(-1, -3, -5)]
#> [1] “two” “four

iii) It is error to mix -ve and +ve value in the subsetiing

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

Subsetting with logical vector keeps all values corresponding to TRUE value(How to perform it?)

A

i) E.g
x [1] 10 3 5 8 1

II. All even (or missing!) values of x
x[x %% 2 == 0]
#> [1] 10 NA 8 NA

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

If u have a named vector , u can subset it with character vector(How to do it?)

A
x  xyz def 
#>   5   2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The simplest type of subsetting is nothing,x[], which returns the completex

A

d) The simplest type of subsetting is nothing,x[], which returns the completex
e)

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

Recursive vectors (lists)

A

to invent something, such as an excuse or a story, often in order to deceive:

I made up an excuse about having to look after the kids.
My dad was always really good at making up stories.

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