Data Visualization and Manipulation

Part 1: Introduction to Tidyverse

Tidyverse

install.packages("tidyverse")
library(tidyverse)

(Remember, you only need to do this once!)

Pipes

# install.packages("gapminder")
library(gapminder)
data(gapminder)

Pipes

Let’s preview the data.

data(gapminder)
head(gapminder)
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.

Pipes

Let’s preview the data.

data(gapminder)
head(gapminder, 10)
## # A tibble: 10 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.

Pipes

Let’s preview the data.

data(gapminder)
head(gapminder, 15)
## # A tibble: 15 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## 11 Afghanistan Asia       2002    42.1 25268405      727.
## 12 Afghanistan Asia       2007    43.8 31889923      975.
## 13 Albania     Europe     1952    55.2  1282697     1601.
## 14 Albania     Europe     1957    59.3  1476505     1942.
## 15 Albania     Europe     1962    64.8  1728137     2313.

Pipes

Let’s preview the data. Some information not showing up.

data(gapminder)
head(gapminder$gdpPercap, 15)
##  [1]  779.4453  820.8530  853.1007  836.1971  739.9811  786.1134  978.0114
##  [8]  852.3959  649.3414  635.3414  726.7341  974.5803 1601.0561 1942.2842
## [15] 2312.8890

Summary

summary(gapminder)
##         country        continent        year         lifeExp     
##  Afghanistan:  12   Africa  :624   Min.   :1952   Min.   :23.60  
##  Albania    :  12   Americas:300   1st Qu.:1966   1st Qu.:48.20  
##  Algeria    :  12   Asia    :396   Median :1980   Median :60.71  
##  Angola     :  12   Europe  :360   Mean   :1980   Mean   :59.47  
##  Argentina  :  12   Oceania : 24   3rd Qu.:1993   3rd Qu.:70.85  
##  Australia  :  12                  Max.   :2007   Max.   :82.60  
##  (Other)    :1632                                                
##       pop              gdpPercap       
##  Min.   :6.001e+04   Min.   :   241.2  
##  1st Qu.:2.794e+06   1st Qu.:  1202.1  
##  Median :7.024e+06   Median :  3531.8  
##  Mean   :2.960e+07   Mean   :  7215.3  
##  3rd Qu.:1.959e+07   3rd Qu.:  9325.5  
##  Max.   :1.319e+09   Max.   :113523.1  
## 

A brief note on factors

str(gapminder)
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
##  $ country  : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ year     : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ lifeExp  : num [1:1704] 28.8 30.3 32 34 36.1 ...
##  $ pop      : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
##  $ gdpPercap: num [1:1704] 779 821 853 836 740 ...

Some Summary Statistics

Below are the mean, median, standard deviation and IQR of the variable “pop”.

# Mean
mean(gapminder$pop)
## [1] 29601212
# Median
median(gapminder$pop)
## [1] 7023596
# Standard Deviation
sd(gapminder$pop)
## [1] 106157897
# Interquartile Range
IQR(gapminder$pop)
## [1] 16791558

Functions in Functions

Both of these are acceptable. However, when we start adding more and more functions, pipes can make your code much easier and more natural to read.

mean(gapminder$pop)
## [1] 29601212

With pipes, we can use

gapminder$pop %>%
  mean()
## [1] 29601212

Functions in Functions

What if we want the mean log population within our dataset? Using base R, we might type:

mean(log(gapminder$pop))
## [1] 15.76611

With pipes, we can use

gapminder$pop %>%
  log() %>%
  mean()
## [1] 15.76611

Filtering Data

head(gapminder$country == "China" & gapminder$year >= 1970)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE

Filtering Data

country_year <- gapminder %>% 
  select(country, year) 
country_year
## # A tibble: 1,704 × 2
##    country      year
##    <fct>       <int>
##  1 Afghanistan  1952
##  2 Afghanistan  1957
##  3 Afghanistan  1962
##  4 Afghanistan  1967
##  5 Afghanistan  1972
##  6 Afghanistan  1977
##  7 Afghanistan  1982
##  8 Afghanistan  1987
##  9 Afghanistan  1992
## 10 Afghanistan  1997
## # … with 1,694 more rows

Filtering Data

china_1980 <- gapminder %>% 
  select(country, year) %>%
filter(country == "China" & year >= 1970)
china_1980
## # A tibble: 8 × 2
##   country  year
##   <fct>   <int>
## 1 China    1972
## 2 China    1977
## 3 China    1982
## 4 China    1987
## 5 China    1992
## 6 China    1997
## 7 China    2002
## 8 China    2007

Part 2: Data Visualization

ggplot2

Learning Objectives

Data Visualization

Data Visualization

# install.packages("ggplot2")
# library(ggplot2)
ggplot(data = gapminder) 

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp)) 

Data Visualization

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp)) +
  geom_point()  

Data Visualization

gapminder_plot <- ggplot(data = gapminder,
       aes(x = year, y = lifeExp)) 

gapminder_plot +
    geom_point()

Building your Plots Iteratively

ggplot(data = gapminder,
       aes(x = year, y = lifeExp)) +
  geom_point(alpha = 0.1)  

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp)) +
  geom_point(color = "red", size = 0.75) 

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp)) +
  geom_point(color = "blue", size = 0.75) 

Data Visualization

ggplot(gapminder,
       aes(year, lifeExp)) +
  geom_point(color = "blue", size = 3) 

Data Visualization

gapminder  %>% 
ggplot(aes(x = year, y = lifeExp)) +
  geom_point(color = "blue", size = 3) 

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp)) +
  geom_line(color = "red") 

Data Visualization

head(CO2) 
##   Plant   Type  Treatment conc uptake
## 1   Qn1 Quebec nonchilled   95   16.0
## 2   Qn1 Quebec nonchilled  175   30.4
## 3   Qn1 Quebec nonchilled  250   34.8
## 4   Qn1 Quebec nonchilled  350   37.2
## 5   Qn1 Quebec nonchilled  500   35.3
## 6   Qn1 Quebec nonchilled  675   39.2

Data Visualization

summary(CO2) 
##      Plant             Type         Treatment       conc          uptake     
##  Qn1    : 7   Quebec     :42   nonchilled:42   Min.   :  95   Min.   : 7.70  
##  Qn2    : 7   Mississippi:42   chilled   :42   1st Qu.: 175   1st Qu.:17.90  
##  Qn3    : 7                                    Median : 350   Median :28.30  
##  Qc1    : 7                                    Mean   : 435   Mean   :27.21  
##  Qc3    : 7                                    3rd Qu.: 675   3rd Qu.:37.12  
##  Qc2    : 7                                    Max.   :1000   Max.   :45.50  
##  (Other):42

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake)) +
  geom_point(color = "red") 

Data Visualization

CO2  %>% 
ggplot(aes(x = conc, y = uptake)) +
  geom_point(color = "red", alpha = 0.5) 

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, group = Treatment)) +
  geom_point() 

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() 

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth(method = lm, se = F)
## `geom_smooth()` using formula = 'y ~ x'

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth(method = lm, se = T)
## `geom_smooth()` using formula = 'y ~ x'

Faceting

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth(method = lm, se = F) + 
  facet_wrap(facets = vars(Type))
## `geom_smooth()` using formula = 'y ~ x'

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth(method = lm, se = F) + 
  facet_wrap(~Type)
## `geom_smooth()` using formula = 'y ~ x'

Faceting

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth(method = lm, se = F) + 
  facet_grid(rows = vars(Type), cols =  vars(Treatment))
## `geom_smooth()` using formula = 'y ~ x'

Faceting

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth(method = lm, se = F) + 
  facet_grid(cols = vars(Type))
## `geom_smooth()` using formula = 'y ~ x'

ggplot2 themes

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth(method = lm, se = T) + 
  facet_wrap(~Type) + 
  labs(title = "Concerntration of CO2") +
  theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

ggplot2 themes

Data Visualization

ggplot(data = CO2,
       aes(x = conc, y = uptake, color = Treatment)) +
  geom_point() +
  geom_smooth(method = lm, se = T) + 
  facet_wrap(~Type) + 
  labs(title = "Concerntration of CO2") +
  theme_bw() + 
  theme(text=element_text(size = 20))
## `geom_smooth()` using formula = 'y ~ x'

Data Visualization (Back to our Example)

ggplot(data = gapminder,
       aes(x = year, y = lifeExp, group = country, color = continent)) +
  geom_line(size = 0.4) 
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp, group = country, color = continent)) +
  geom_line(size = 0.4) + 
geom_smooth(aes(group = continent), 
              se = FALSE, size = 1.5, 
              color = "black", 
              method = "loess") +
  facet_wrap(~ continent) 
## `geom_smooth()` using formula = 'y ~ x'

Data Visualization

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp,
           group = country,
           color = continent)) + 
  geom_line(size = 0.5) +
  geom_smooth(aes(group = continent), 
              se = FALSE, size = 1.5, 
              color = "black", 
              method = "loess") +
  facet_wrap(~ continent) +
  labs(title = "Life expectancy over time by continent", 
       x = "Year", y = "Life Expectancy", 
       legend = "Continent")

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp,
           group = country,
           color = continent)) + 
  geom_line(size = 0.5) +
  geom_smooth(aes(group = continent), 
              se = FALSE, size = 1.5, 
              color = "black", 
              method = "loess") +
  facet_wrap(~ continent) +
  labs(title = "Life expectancy over time by continent", 
       x = "Year", y = "Life Expectancy", 
       legend = "Continent") 

Data Visualization

ggplot(data = gapminder,
       aes(x = year, y = lifeExp,
           group = country,
           color = continent)) + 
  geom_line(size = 0.5) +
  geom_smooth(aes(group = continent), 
              se = FALSE, size = 1.5, color = "black", method = "loess") +
  facet_wrap(~ continent) +
  labs(title = "Life expectancy over time by continent", 
       x = "Year", y = "Life Expectancy") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
        legend.position = "none") 

Data Visualization

Great, that looks good! Now we can store it as an object.

lifeExp_plot <- ggplot(data = gapminder, 
                       aes(x = year, y = lifeExp,
                           group = country,
                           color = continent)) + 
  geom_line(size = 0.5) +
  geom_smooth(aes(group = continent), 
              se = FALSE, size = 1.5, color = "black", method = "loess") +
  facet_wrap(~ continent) +
  labs(title = "Life expectancy over time by continent", 
       x = "Year", y = "Life Expectancy") +
  theme_bw(base_size = 16) + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold"), 
        legend.position = "none",
        axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
        panel.spacing.x = unit(0.75, "cm")) 

Data Visualization

Data Visualization

ggplot(data = gapminder, 
       aes(x = lifeExp))

Data Visualization

ggplot(data = gapminder, 
       aes(x = lifeExp)) + 
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Data Visualization

ggplot(data = gapminder, 
       aes(x = lifeExp)) + 
  geom_histogram(binwidth = 1, 
                 color = "black", 
                 fill = "lightblue") 

Data Visualization

ggplot(data = gapminder, 
       aes(x = lifeExp)) + 
  geom_histogram(binwidth = 1, 
                 color = "black", 
                 fill = "lightblue") +
  theme_bw(base_size = 20) +
  labs(x = "Life Expectancy", 
       y = "Count")

Data Visualization

ggplot(data = gapminder, 
       aes(x = continent, y = lifeExp)) 

Data Visualization

ggplot(data = gapminder, 
       aes(x = continent, y = lifeExp)) + 
geom_boxplot()

Data Visualization

ggplot(data = gapminder, 
       aes(x = continent, y = lifeExp)) + 
geom_boxplot(fill = "lightblue")

Data Visualization

ggplot(data = gapminder, 
       aes(x = continent, y = lifeExp)) + 
geom_boxplot(fill = "lightblue") + 
    labs(title = "Life expectancy by Continent", 
       x = "", 
       y = "") +
  theme(plot.title =
          element_text(hjust = 0.5)) +
  ylim(c(20, 85)) 

Data Visualization

ggplot(data = gapminder, 
       aes(x = continent, y = lifeExp)) + 
geom_boxplot(fill = "lightblue") + 
    labs(title = "Life expectancy by Continent", 
       x = "", 
       y = "") +
  theme(plot.title =
          element_text(hjust = 0.5)) +
  ylim(c(0, 85)) 

Data Visualization Suggestions

*Don’ts

*Do’s

Data Visualization cheatsheet

What plot to use?

One numeric variable: Histogram

ggplot(gapminder, aes(x = lifeExp)) + 
  geom_histogram() + 
  labs(x = "Life Expectancy",
       y = "Count",
       title = "Distribution of Life Expectancy") + 
  theme_bw(base_size = 16) + 
  theme(plot.title = 
          element_text(hjust = 0.5)) 

One numeric variable: Boxplot

ggplot(gapminder, aes(y = lifeExp)) + 
  geom_boxplot() + 
  labs(x = "",
       y = "Life Expectancy",
       title = "Distribution of Life Expectancy") + 
  theme_bw(base_size = 16) + 
  theme(plot.title = 
          element_text(hjust = 0.5),
        axis.ticks.x = element_blank(),
        axis.text.x = element_blank()) +
  ylim(c(0,90))

One categorical variable: Bar chart

ggplot(gapminder, aes(x = continent)) + 
  geom_bar() + 
  labs(x = "Continent",
       y = "Count",
       title = "Distribution of Continent") + 
  theme_bw(base_size = 16) + 
  theme(plot.title = 
          element_text(hjust = 0.5)) 

One numeric and one categorical variable

Here, we have multiple observations for each category.

ggplot(gapminder, aes(x = continent, y = lifeExp)) + 
  geom_boxplot() + 
  labs(x = "Continent",
       y = "Life Expectancy",
       title = "Life Expectancy by Continent") + 
  theme_bw(base_size = 16) + 
  theme(plot.title = 
          element_text(hjust = 0.5)) 

One numeric and one categorical variable

mean_lifeExp <- gapminder %>%
  group_by(continent) %>%
  summarise(mean_lifeExp = mean(lifeExp))

ggplot(mean_lifeExp, aes(x = continent, y = mean_lifeExp)) + 
  geom_bar(stat = "identity") + 
  labs(x = "Continent",
       y = "Mean Life Expectancy",
       title = "Mean Life Expectancy by Continent") + 
  theme_bw(base_size = 16) + 
  theme(plot.title = 
          element_text(hjust = 0.5)) 

Two numeric variables: scatterplot

ggplot(gapminder, aes(y = lifeExp, x = log(gdpPercap))) + 
  geom_point() + 
  labs(y = "Life expectancy",
       x = "Log GDP per capita",
       title = "Life Expectancy by Log GDP per capita") + 
  ylim(c(0,90)) + 
  theme_bw(base_size = 16) + 
  theme(plot.title = 
          element_text(hjust = 0.5)) 

Two categorical variables

gapminder <- gapminder %>% 
  mutate(lifeExpCat = ifelse(lifeExp > 60, TRUE, FALSE))
ggplot(gapminder, aes(x = continent, fill = lifeExpCat)) + 
  geom_bar() + 
  xlab("Continent") + 
  ylab("Count") + 
  labs(fill = "Life Exp. > 60") + 
  ggtitle("Distribution of Continent and Life Expectancy") + 
  theme_bw(base_size = 16) + 
  theme(plot.title = 
          element_text(hjust = 0.5)) 

Part 3: Data Manipulation

Recall Data

Let’s preview the data.

data(gapminder)
head(gapminder)
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.

select(): Column selection

gapminder %>% 
  select(lifeExp, gdpPercap)
## # A tibble: 1,704 × 2
##    lifeExp gdpPercap
##      <dbl>     <dbl>
##  1    28.8      779.
##  2    30.3      821.
##  3    32.0      853.
##  4    34.0      836.
##  5    36.1      740.
##  6    38.4      786.
##  7    39.9      978.
##  8    40.8      852.
##  9    41.7      649.
## 10    41.8      635.
## # … with 1,694 more rows

select(): Column selection

Use select() to choose certain columns. We can also use select() to remove certain columns by putting a - symbol in front.

gapminder %>% 
  select(-lifeExp, -gdpPercap)
## # A tibble: 1,704 × 4
##    country     continent  year      pop
##    <fct>       <fct>     <int>    <int>
##  1 Afghanistan Asia       1952  8425333
##  2 Afghanistan Asia       1957  9240934
##  3 Afghanistan Asia       1962 10267083
##  4 Afghanistan Asia       1967 11537966
##  5 Afghanistan Asia       1972 13079460
##  6 Afghanistan Asia       1977 14880372
##  7 Afghanistan Asia       1982 12881816
##  8 Afghanistan Asia       1987 13867957
##  9 Afghanistan Asia       1992 16317921
## 10 Afghanistan Asia       1997 22227415
## # … with 1,694 more rows

select(): Column selection

names(gapminder)
## [1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"
gapminder %>%
  select(starts_with("co")) %>%
  head()
## # A tibble: 6 × 2
##   country     continent
##   <fct>       <fct>    
## 1 Afghanistan Asia     
## 2 Afghanistan Asia     
## 3 Afghanistan Asia     
## 4 Afghanistan Asia     
## 5 Afghanistan Asia     
## 6 Afghanistan Asia

select(): Column selection

names(gapminder)
## [1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"
gapminder %>%
  select(ends_with("p")) %>%
  head()
## # A tibble: 6 × 3
##   lifeExp      pop gdpPercap
##     <dbl>    <int>     <dbl>
## 1    28.8  8425333      779.
## 2    30.3  9240934      821.
## 3    32.0 10267083      853.
## 4    34.0 11537966      836.
## 5    36.1 13079460      740.
## 6    38.4 14880372      786.

select(): Column selection

names(gapminder)
## [1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"
gapminder %>%
  select(contains("Per")) %>%
  head()
## # A tibble: 6 × 1
##   gdpPercap
##       <dbl>
## 1      779.
## 2      821.
## 3      853.
## 4      836.
## 5      740.
## 6      786.

select(): Column selection

mutate(): create new columns

gapminder %>%
  mutate(gdp = gdpPercap * pop) %>%
  head()
## # A tibble: 6 × 7
##   country     continent  year lifeExp      pop gdpPercap          gdp
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>        <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.  6567086330.
## 2 Afghanistan Asia       1957    30.3  9240934      821.  7585448670.
## 3 Afghanistan Asia       1962    32.0 10267083      853.  8758855797.
## 4 Afghanistan Asia       1967    34.0 11537966      836.  9648014150.
## 5 Afghanistan Asia       1972    36.1 13079460      740.  9678553274.
## 6 Afghanistan Asia       1977    38.4 14880372      786. 11697659231.

mutate(): create new columns

gapminder %>%
  mutate(gdp = gdpPercap * pop,
         logGDP = log(gdp)) %>% 
  head()
## # A tibble: 6 × 8
##   country     continent  year lifeExp      pop gdpPercap          gdp logGDP
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>        <dbl>  <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.  6567086330.   22.6
## 2 Afghanistan Asia       1957    30.3  9240934      821.  7585448670.   22.7
## 3 Afghanistan Asia       1962    32.0 10267083      853.  8758855797.   22.9
## 4 Afghanistan Asia       1967    34.0 11537966      836.  9648014150.   23.0
## 5 Afghanistan Asia       1972    36.1 13079460      740.  9678553274.   23.0
## 6 Afghanistan Asia       1977    38.4 14880372      786. 11697659231.   23.2

split-apply-combine

split-apply-combine

grouped_gap <- gapminder %>%
  group_by(continent)
head(grouped_gap, 4)
## # A tibble: 4 × 6
## # Groups:   continent [1]
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.

split-apply-combine

grouped_gap <- gapminder %>%
  group_by(continent)
tail(grouped_gap, 4)
## # A tibble: 4 × 6
## # Groups:   continent [1]
##   country  continent  year lifeExp      pop gdpPercap
##   <fct>    <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Zimbabwe Africa     1992    60.4 10704340      693.
## 2 Zimbabwe Africa     1997    46.8 11404948      792.
## 3 Zimbabwe Africa     2002    40.0 11926563      672.
## 4 Zimbabwe Africa     2007    43.5 12311143      470.

split-apply-combine

ungrouped_gap <- ungroup(grouped_gap)
head(ungrouped_gap)
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.

split-apply-combine

gapminder %>%
  group_by(continent) %>%
  summarize(mean_lifeExp = mean(lifeExp, na.rm = TRUE),
            max_gdp = max(gdpPercap))
## # A tibble: 5 × 3
##   continent mean_lifeExp max_gdp
##   <fct>            <dbl>   <dbl>
## 1 Africa            48.9  21951.
## 2 Americas          64.7  42952.
## 3 Asia              60.1 113523.
## 4 Europe            71.9  49357.
## 5 Oceania           74.3  34435.

split-apply-combine

gapminder %>%
  group_by(continent) %>%
  summarize(summary_lifeExp = mean(lifeExp, na.rm = TRUE), median(lifeExp, na.rm = TRUE), sd(lifeExp, na.rm = TRUE))
## # A tibble: 5 × 4
##   continent summary_lifeExp `median(lifeExp, na.rm = TRUE)` sd(lifeExp, na.rm …¹
##   <fct>               <dbl>                           <dbl>                <dbl>
## 1 Africa               48.9                            47.8                 9.15
## 2 Americas             64.7                            67.0                 9.35
## 3 Asia                 60.1                            61.8                11.9 
## 4 Europe               71.9                            72.2                 5.43
## 5 Oceania              74.3                            73.7                 3.80
## # … with abbreviated variable name ¹​`sd(lifeExp, na.rm = TRUE)`

split-apply-combine

gapminder %>%
  group_by(continent) %>%
  summarize(mean_lifeExp = mean(lifeExp, na.rm = TRUE),
            max_gdp = max(gdpPercap)) %>%
  arrange(mean_lifeExp)
## # A tibble: 5 × 3
##   continent mean_lifeExp max_gdp
##   <fct>            <dbl>   <dbl>
## 1 Africa            48.9  21951.
## 2 Asia              60.1 113523.
## 3 Americas          64.7  42952.
## 4 Europe            71.9  49357.
## 5 Oceania           74.3  34435.

split-apply-combine

gapminder %>%
  group_by(continent) %>%
  summarize(mean_lifeExp = mean(lifeExp, na.rm = TRUE),
            max_gdp = max(gdpPercap)) %>%
  arrange(desc(mean_lifeExp))
## # A tibble: 5 × 3
##   continent mean_lifeExp max_gdp
##   <fct>            <dbl>   <dbl>
## 1 Oceania           74.3  34435.
## 2 Europe            71.9  49357.
## 3 Americas          64.7  42952.
## 4 Asia              60.1 113523.
## 5 Africa            48.9  21951.

pull(): extract column as vector

gapminder %>%
  select(lifeExp) %>%
  head(4)
## # A tibble: 4 × 1
##   lifeExp
##     <dbl>
## 1    28.8
## 2    30.3
## 3    32.0
## 4    34.0
gapminder %>%
  pull(lifeExp) %>%
  head(10)
##  [1] 28.801 30.332 31.997 34.020 36.088 38.438 39.854 40.822 41.674 41.763

pull(): extract column as vector

gapminder %>%
  select(continent) %>%
  head(4)
## # A tibble: 4 × 1
##   continent
##   <fct>    
## 1 Asia     
## 2 Asia     
## 3 Asia     
## 4 Asia
gapminder %>%
  select(continent) %>%
  head(20)
## # A tibble: 20 × 1
##    continent
##    <fct>    
##  1 Asia     
##  2 Asia     
##  3 Asia     
##  4 Asia     
##  5 Asia     
##  6 Asia     
##  7 Asia     
##  8 Asia     
##  9 Asia     
## 10 Asia     
## 11 Asia     
## 12 Asia     
## 13 Europe   
## 14 Europe   
## 15 Europe   
## 16 Europe   
## 17 Europe   
## 18 Europe   
## 19 Europe   
## 20 Europe

rename(): …renaming!

data(gapminder)
head(gapminder)
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.

rename(): …renaming!

gapminder %>%
  rename(life_exp = lifeExp)
## # A tibble: 1,704 × 6
##    country     continent  year life_exp      pop gdpPercap
##    <fct>       <fct>     <int>    <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952     28.8  8425333      779.
##  2 Afghanistan Asia       1957     30.3  9240934      821.
##  3 Afghanistan Asia       1962     32.0 10267083      853.
##  4 Afghanistan Asia       1967     34.0 11537966      836.
##  5 Afghanistan Asia       1972     36.1 13079460      740.
##  6 Afghanistan Asia       1977     38.4 14880372      786.
##  7 Afghanistan Asia       1982     39.9 12881816      978.
##  8 Afghanistan Asia       1987     40.8 13867957      852.
##  9 Afghanistan Asia       1992     41.7 16317921      649.
## 10 Afghanistan Asia       1997     41.8 22227415      635.
## # … with 1,694 more rows