Intro to Coding
Part 3: Vectors and Data Frames
Vectors and Data Frames: things that contain many things
Vectors
Say you want to store the cost (in dollars) of some grocery items. You could do this:
But this gets messy in a hurry. You have to keep track of five different variables here. Want to know the total cost of items in your cart? Well, get ready to write a long line of code like:
Or maybe:
This spirals out of hand quickly if you want to add 20 more things to your cart.
You really want to be able to group these things together somehow, in one variable, something like a box that can hold many things. Luckily R (and every programming langauge) has something exactly for this.
In R it’s called a “vector.” It is simply a long “box” with a bunch of slots to put stuff in. To make such a box-of-stuff, you use the function called c (I think it stands for “combine”, but it’s how you make a vector):
c is a special function that can take any number of arguments. You can even use it to combine two vectors into a single one:
Vectors are useful for a lot of reasons, including the fact that some functions expect a vector as an input. For example, mean is a function in R that takes the average of some numbers. But you can’t just pass it raw numbers: mean(2, 3, 4) will not work.
Instead, mean expects one argument – a vector, full of numbers. Like so:
We called the mean function, passed it one argument (a vector of numbers, from the variable named costs), and it returned 2.938. So your average grocery item costs about $3.
The function sum can also take a single argument, a vector of numbers:
Again: we called the sum function, passed it one argument (a vector of numbers, stored in the variable costs), and it returned 14.68. Your total grocery bill is $14.68.
Data Frames: going two dimensional
A data frame is like a spreadsheet or table: it has rows and columns. The data you work with in this class will overwhelmingly be in data frames, so you will become very comfortable with them.
Most datasets in this class are ready for you once you load the stat20data library. Then you’ll be able to use variables like penguins and flights and promote, all of which refer to data frames full of data.
Let’s peek at a few of those. Below we use the head function to get just the first few rows of a dataframe (so it doesn’t try to print hundreds of rows of data).
library(stat20data)
head(penguins)# A tibble: 6 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 40.3 18 195 3250
4 Adelie Torgersen 36.7 19.3 193 3450
5 Adelie Torgersen 39.3 20.6 190 3650
6 Adelie Torgersen 38.9 17.8 181 3625
# ℹ 2 more variables: sex <fct>, year <int>
head(flights)# A tibble: 6 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2020 1 1 8 2359 9 528 532
2 2020 1 1 29 39 -10 356 420
3 2020 1 1 37 40 -3 846 856
4 2020 1 1 41 45 -4 908 913
5 2020 1 1 44 2300 104 834 709
6 2020 1 1 48 56 -8 641 658
# ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <dbl>,
# tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
# hour <dbl>, minute <dbl>, time_hour <dttm>
head(promote) id gender decision
1 1 female promote
2 2 male promote
3 3 male promote
4 4 female not promote
5 5 male promote
6 6 female promote
You can also create data frames manually.
This is useful for quick work where you only have a little bit of data.
First, you put each column of data in its own vector. Then you put all the vectors into a data frame. Like so:
names <- c("Alice", "Bob", "Carla")
ages <- c(19, 20, 21)
major <- c("Economics", "Biology", "English")
people <- data.frame(names, ages, major)
people names ages major
1 Alice 19 Economics
2 Bob 20 Biology
3 Carla 21 English