Chapter21 : Vectors Flashcards
What are the two types of Vectors?
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
What is diff btweem atomic vectors and list vectors?
The chief difference between atomic vectors and lists is that atomic vectors arehomogeneous, while lists can beheterogeneous
What are two key properties every vector has?
Itstype, which you can determine withtypeof().
Itslength, which you can determine withlength()
What are Numeric vector?
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
What are character vector?
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
What is Coercion?
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.
What are test functions in R?
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
Scalars and recycling rules
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 to named vectors
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.
Vector subsetting. Wat are Four ways to subset a vector?
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 to use numeric vector containing only integer to access vector?
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
Subsetting with logical vector keeps all values corresponding to TRUE value(How to perform it?)
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
If u have a named vector , u can subset it with character vector(How to do it?)
x xyz def #> 5 2
The simplest type of subsetting is nothing,x[], which returns the completex
d) The simplest type of subsetting is nothing,x[], which returns the completex
e)
Recursive vectors (lists)
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.