Created
January 1, 2021 18:33
-
-
Save luisDVA/0d05853eafee2b4c789b573a5e942056 to your computer and use it in GitHub Desktop.
Useful dplyr functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## %######################################################%## | |
# # | |
#### 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