Useful Functions

load libraries

library(tidyverse)
library(psych)
library(palmerpenguins)
library(janitor)
library(gt)
library(papaja)
library(ggeasy)
library(patchwork)
library(ggbeeswarm)

case_when()

x = 11:60

mydata <- tibble(
  id = 1:50, 
  age = sample(x)
)

range(mydata$age)
## [1] 11 60
mydata %>%
  mutate(age_groups = case_when(age < 35 ~ "Younger",
                                age >= 35 ~ "Older"))
## # A tibble: 50 x 3
##       id   age age_groups
##    <int> <int> <chr>     
##  1     1    15 Younger   
##  2     2    54 Older     
##  3     3    23 Younger   
##  4     4    11 Younger   
##  5     5    55 Older     
##  6     6    12 Younger   
##  7     7    21 Younger   
##  8     8    41 Older     
##  9     9    48 Older     
## 10    10    35 Older     
## # ... with 40 more rows

sd and se

library(palmerpenguins)
penguins <- penguins %>%
  na.omit()

# get mean, sd, n, se body mass for males and females
penguins %>%
  group_by(sex) %>%
  summarise(mean_bm = mean(body_mass_g),
            sd_bm = sd(body_mass_g),
            n_bm = n(),
            se = sd_bm/sqrt(n_bm))
## # A tibble: 2 x 5
##   sex    mean_bm sd_bm  n_bm    se
##   <fct>    <dbl> <dbl> <int> <dbl>
## 1 female   3862.  666.   165  51.9
## 2 male     4546.  788.   168  60.8

summarise across & mutate across

penguins %>%
  summarise(across(contains("mm"), list(mean_peng = mean, sd_peng = sd), na.rm=TRUE))
## # A tibble: 1 x 6
##   bill_length_mm_mean_peng bill_length_mm_sd_~ bill_depth_mm_m~ bill_depth_mm_s~
##                      <dbl>               <dbl>            <dbl>            <dbl>
## 1                     44.0                5.47             17.2             1.97
## # ... with 2 more variables: flipper_length_mm_mean_peng <dbl>,
## #   flipper_length_mm_sd_peng <dbl>
penguins %>%
  mutate(across(species:island, as.character))
## # A tibble: 333 x 8
##    species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
##    <chr>   <chr>              <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
##  7 Adelie  Torgersen           39.2          19.6               195        4675
##  8 Adelie  Torgersen           41.1          17.6               182        3200
##  9 Adelie  Torgersen           38.6          21.2               191        3800
## 10 Adelie  Torgersen           34.6          21.1               198        4400
## # ... with 323 more rows, and 2 more variables: sex <fct>, year <int>

Functions

why use functions?

  • avoid copy and pasting
  • you can add as many arguments as you like and then reuse the function over and over
penguins <- penguins %>%
  na.omit()

# x axis = bill length
# y axis = bill depth
# title = bill length x bill depth
penguins1 <- ggplot(penguins, aes(bill_length_mm, bill_depth_mm, fill = species)) +
  geom_boxplot() +
  theme_bw() +
  labs(title = "Bill Length x Bill Depth") +
  easy_remove_legend()

# x axis = bill length
# y axis = flipper length
# title = bill length x flipper length
penguins2 <- ggplot(penguins, aes(bill_length_mm, flipper_length_mm, fill = species)) +
  geom_boxplot() +
  theme_bw() +
  labs(title = "Bill Length x Flipper Length") +
  easy_remove_legend()

penguins_fun <- function(y_var, plot_title="TEST TITLE", ylim1, ylim2) {
  
  penguins_plot <- ggplot(penguins, aes(bill_length_mm, y_var, fill = species)) +
    geom_boxplot() +
    theme_bw() +
    labs(title = plot_title) +
    ylim(ylim1,ylim2) +
    easy_remove_legend()
  
  return(penguins_plot)
  
}

plot1 <- penguins_fun(y_var = penguins$flipper_length_mm, plot_title = "Bill length x flipper length", ylim1 = 150, ylim2 = 250)
plot2 <- penguins_fun(y_var = penguins$bill_depth_mm, plot_title = "bill length x bill depth", ylim1 = 10, ylim2 = 25)

plot1 + plot2