dplyr
After today’s session you will be able to:
dplyr
functions
%>%
)
# Load data
df_v1 <- read.csv("butterfly.csv")
# Subset to only one treatment
df_v2 <- filter(df_v1, treatment == "cows")
# Add together caterpillars and adult butterflies
df_v3 <- mutate(df_v2, monarch.tot = monarch.bfly + monarch.larva)
# Keep only the total monarch column
df_v4 <- select(df_v3, monarch.tot)
# Load data
df_v1 <- read.csv("butterfly.csv")
# Subset to only one treatment
df_v2 <- filter(df_v1, treatment == "cows")
# Add together caterpillars and adult butterflies
df_v3 <- mutate(df_v2, monarch.tot = monarch.bfly + monarch.larva)
# Keep only the total monarch column
df_v4 <- select(df_v3, monarch.tot)
# Load data
df_v1 <- read.csv("butterfly.csv")
# Do all needed wrangling
df_done <- df_v1 %>%
# Subset to only one treatment
filter(treatment == "cows") %>%
# Add together caterpillars and adult butterflies
mutate(monarch.tot = monarch.bfly + monarch.larva) %>%
# Keep only the total monarch column
select(monarch.tot)
René Magritte – The Treachery of Images (1929)
magrittr
package
%>%
dplyr
offers three functions to accomplish this
group_by
summarize
ungroup
group_by
has similar structure to select
summarize
has similar structure to mutate
new_column = function(old_column)
ungroup
has no arguments!
mean
na.rm
argument that determines whether missing values are included
sd
min
and max
Let’s check out an example:
group_by
summarize
palmerpenguins
package, answer the following questions:
filter
before or after summarize
!
After completing this assignment you will be able to:
stringr
– Simple, Consistent Wrappers for Common String Operations
dndR
– Dungeons & Dragons Functions for Players and Dungeon Masters
lterpalettefinder
– Extract Color Palettes from Photos and Pick Official LTER Palettes
supportR
– Support Functions for Wrangling and Visualization
vegan
– Community Ecology Package
dplyr
– A Grammar of Data Manipulation
tidyr
– Tidy Messy Data
pivot_longer
& pivot_wider
ggplot2
– Create Elegant Data Visualizations Using the Grammar of Graphics
After this bonus session you will be able to:
tidyr::pivot_longer
data
= the wide data to pivot
cols
= the columns to pivot
cols = colD:colX
cols = -colA:-colC
names_to
= name of new column to hold old column names
values_to
= name of new column to hold values
pivot_longer
read.csv
tidyr::pivot_wider
data
= the wide data to pivot
names_from
= name of the column to turn into new column names
values_from
= name of column to make into new column values
values_fill
= value to fill if value is missing in original data
pivot_wider
pivot_wider
!
read.csv
?
NA
(not 0
)