Last active
November 2, 2022 09:29
-
-
Save ivopbernardo/245e39727c5250b5a4319376e5d79f0a to your computer and use it in GitHub Desktop.
Some functions from the dplyr Library
This file contains hidden or 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
# dplyr library example used in blog post: | |
# https://towardsdatascience.com/8-cool-dplyr-function-to-learn-in-r-8736d7fa899c | |
library(dplyr) | |
starwars_df <- starwars | |
# Filter using Dplyr | |
filter_droids <- starwars %>% | |
filter(species == 'Droid') | |
# Filter using R Base | |
starwars[starwars$species == 'Droid',] | |
# Multiple Condition Filter | |
filter_droids_gold <- starwars %>% | |
filter(species == 'Droid', skin_color == 'gold') | |
# Sorting Data using Arrange | |
sorted_height <- starwars %>% | |
arrange(height) | |
# Sorting Data using Arrange - Descending Order | |
reverse_sorted_height <- starwars %>% | |
arrange(-height) | |
# Base R Example of Sort | |
starwars[order(starwars$height),] | |
# Multiple Sorting using Arrange | |
sorted_hair_height <- starwars %>% | |
arrange(hair_color, height) | |
# Create new Column Height Times Mass | |
starwars_df <- starwars %>% | |
mutate(height_x_mass = height*mass) | |
# Mutate, multiple columns | |
starwars_df <- starwars %>% | |
mutate(height_x_mass = height*mass, | |
franchise = 'Star Wars') | |
# Sample_N and Sample_Frac | |
starwars_sample_n <- starwars %>% | |
sample_n(size=5) | |
starwars_sample_frac <- starwars %>% | |
sample_frac(0.02) | |
# Mean using Base R | |
mean(starwars$height, na.rm=TRUE) | |
# Summarise | |
starwars %>% | |
summarise(height_mean = mean(height, na.rm = TRUE)) | |
# Summarise using max | |
starwars %>% | |
summarise(height_max = max(height, na.rm = TRUE)) | |
# Summarise by groups | |
mean_height_by_species <- starwars %>% | |
group_by(species) %>% | |
summarise(height_mean = mean(height, na.rm = TRUE)) | |
# Creating Origin DataFrame | |
species_origin <- data.frame( | |
species = c('Human','Ewok'), | |
origin = c('Earth','Endor') | |
) | |
# Inner Join | |
starwars_inner <- starwars %>% | |
inner_join(species_origin, on='Species') | |
# Left Join | |
starwars_left <- starwars %>% | |
left_join(species_origin, on='Species') | |
# Right Join | |
starwars_right <- starwars %>% | |
right_join(species_origin, on='Species') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment