Vectors
Vectors Basics
- A vector is a sequence of values that all have the same type
 - You can create a vector using the 
c()function, which stands for “combine” 
- Using the 
strfunction we learned last time shows that this is a vector of 4 character strings 
You can select pieces of a vector by “slicing” the vector (like slicing a pizza). This is done using square brackets []. In general [] in R means, “give me a piece of something”.
If we put one number in the brackets it will us the value that position:
Try changing this to get the values at different positions.
If we use two numbers separated by a colon this will give us all the values in the range of those numbers. For 1:3 will use us the first through third values.
1:3works by makeing a vector of the whole numbers 1 through 3.So, this is the same as
states[1:3]is the same asstates[c(1, 2, 3)]You can use a vector to get any subset or order you want
states[c(4, 1, 3)]Many functions in R take a vector as input and return a value
This includes the function
lengthwhich determines how many items are in a vector
- We can also calculate common summary statistics
 - For example, if we have a vector of population counts
 
Do Basic Vectors.
Null values
- So far we’ve worked with vectors that contain no missing values
 - But most real world data has values that are missing for a variety of reasons
 - For example, kangaroo rats don’t like being caught by humans and are pretty good at escaping before you’ve finished measuring them
 - Missing values, known as “null” values, are written in R as 
NAwith no quotes, which is short for “not available” - So a vector of 4 population counts with the third value missing would look like
 
- If we try to take the mean of this vector we get 
NA? 
- Hard to say what a calculation including 
NAshould be - So most calculations return 
NAwhenNAis in the data - Can tell many functions to remove the 
NAbefore calculating - Do this using an optional argument, which is an argument that we don’t have to include unless we want to modify the default behavior of the function
 - Add optional arguments by providing their name (
na.rm),=, and the value that we want those arguments to take (TRUE) 
Do Nulls in Vectors.
Working with multiple vectors
- Build on example where we have information on states and population counts by adding areas
 
Vector math
- We can divide the count vector by the area vector to get a vector of the density of individuals in that area
 
- This works because when we divide vectors, R divides the first value in the first vector by the first value in the second vector, then divides the second values in each vector, and so on
 - Element-wise: operating on one element at a time
 
Filtering
- Subsetting or “filtering” is done using 
[] - Like with slicing, the 
[]say “give me a piece of something” - Selects parts of vectors based on “conditions” not position
 - Get the density values in site a
 
==is how we indicate “equal to” in most programming languages.Not
=.=is used for assignment.Can also do “not equal to”
- Numerical comparisons like greater or less than
 - Select states that meet with some restrictions on density
 
- Can subset a vector based on itself
 - If we want to look at the densities greater than 3
 densityis both the vector being subset and part of the condition
- Multiple vectors can be used together to perform element-wise math, where we do the same calculation for each position in the vectors
 - We can also filter the values in vector based on the values in another vector or itself