# _____
# | __ \
# | |__) |
# | _ /
# | | \ \
# |_| \_\ Programming Language
# Some data 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)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.4
## ✓ tibble 3.0.1 ✓ dplyr 0.8.5
## ✓ tidyr 1.0.3 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
# 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.