Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Created January 1, 2021 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisDVA/0d05853eafee2b4c789b573a5e942056 to your computer and use it in GitHub Desktop.
Save luisDVA/0d05853eafee2b4c789b573a5e942056 to your computer and use it in GitHub Desktop.
Useful dplyr functions
## %######################################################%##
# #
#### Useful dplyr functions - your turn ####
# #
## %######################################################%##
# Load the mammal sleep data bundled with ggplot2
# Select "name" and "conservation" columns and those that include the string 'sleep' in their name
# Create a new column that contains the values of 'sleep_total' multiplied by 3
# Filter the data to remove domesticated mammals
# Use pipes to chain the operations
# load packages -----------------------------------------------------------
library(dplyr)
library(ggplot2)
# load data ---------------------------------------------------------------
data("msleep")
# transform data ----------------------------------------------------------
# Select "name" and "conservation" columns and those that include the string 'sleep' in their name
# explore variable names
names(msleep)
# Select columns
msleep %>% select(name, conservation, sleep_total, sleep_rem, sleep_cycle)
# Create a new column that contains the values of 'sleep_total' multiplied by 3
msleep %>%
select(name, conservation, sleep_total, sleep_rem, sleep_cycle) %>%
mutate(sleep3 = sleep_total * 3)
# Filter the data to remove domesticated mammals
msleep %>%
select(name, conservation, sleep_total, sleep_rem, sleep_cycle) %>%
mutate(sleep3 = sleep_total * 3) %>%
filter(conservation != "domesticated")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment