Skip to content

Instantly share code, notes, and snippets.

@mxeliezer
Created May 29, 2020 17:53
Show Gist options
  • Save mxeliezer/7ebef499908026b24ca8d15f66631024 to your computer and use it in GitHub Desktop.
Save mxeliezer/7ebef499908026b24ca8d15f66631024 to your computer and use it in GitHub Desktop.
R script with some tidyverse selection and filtering tips.
# _____
# | __ \
# | |__) |
# | _ /
# | | \ \
# |_| \_\ Programming Language
# Some data selection and filtering tips using R programming.
# And the tidyverse library to filter and subset our data.
# Exercise from 'R Programming 101' youtube channel (org. Greg Martin).
# Library used
library(tidyverse)
# Tidtverse builtin data set
# Many characteristics about some animals species.
view(msleep)
# Selecting and filtering
# Example I
my_data <- msleep %>%
select(name, sleep_total) %>%
filter(sleep_total > 18)
# Example II
my_data <- msleep %>%
select(name, order, bodywt, sleep_total) %>%
filter(order == "Primates", bodywt > 20)
# Example III
my_data <- msleep %>%
select(name, sleep_total) %>%
filter(name %in% c("Cow", "Dog", "Horse"))
# Example IV
my_data <- msleep %>%
select(name, sleep_total) %>%
filter(between(sleep_total, 16, 18))
# Example V
my_data <- msleep %>%
select(name, sleep_total) %>%
filter(near(sleep_total, 17, tol = 0.5))
# Example VI
my_data <- msleep %>%
select(name, conservation, sleep_total) %>%
filter(is.na(conservation)) #locating the missing values.
# Example VII
my_data <- msleep %>%
select(name, conservation, sleep_total) %>%
filter(!is.na(conservation)) #seeing data without the missing values.
@mxeliezer
Copy link
Author

URL from original tutorial R Programming 101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment