library(dplyr)
library(DT)
msleep <- read.csv("msleep_ggplot2.csv")

1 Select

1.1 Select Column

Select Column sleep_total

sleep_total <- msleep %>% 
  select(sleep_total)
datatable(sleep_total, options = list(pageLength = 5))

1.2 Select column starting with

Select all the columns starting with sl

starts_sl <- msleep %>% 
  select(starts_with("sl"))
datatable(starts_sl, options = list(pageLength = 5))

2 Group by & Summarise

2.1 Average

Find the average sleep of each animal type

msleep_meanTotal <- msleep %>% 
  group_by(vore) %>% 
  summarise(mean(sleep_total))
datatable(msleep_meanTotal, options = list(pageLength = 5))

3 Filter

3.1 Basic Filter

  • Filter the animals that sleep more than 2 hours
msleep_more2hours <- msleep %>% 
  filter(sleep_total > 2)

datatable(msleep_more2hours, options = list(pageLength = 5, scrollX='400px'))
  • Filter the animals that sleep more than 2 hours or less than 19 hours
msleep_between_2_19 <- msleep %>%
  filter(sleep_total > 2 | sleep_total < 19)
datatable(msleep_between_2_19, options = list(pageLength = 5, scrollX='400px'))

3.2 Between, And, Not equal

  • Filter the animals that sleep between 2 and 19 hours, and they are NOT domesticated
msleep_2_19_notdom <- msleep %>%
  filter(between(sleep_total, 2, 19) & conservation != "domesticated") 
datatable(msleep_2_19_notdom, options = list(pageLength = 5, scrollX='400px'))
  • Check if your filter is filtering NA for the variable conservation. If not, filter them.
msleep %>%
  filter(between(sleep_total, 2, 19) & conservation != "domesticated" & conservation == "NA") 
##  [1] X            name         genus        vore         order       
##  [6] conservation sleep_total  sleep_rem    sleep_cycle  awake       
## [11] brainwt      bodywt      
## <0 rows> (or 0-length row.names)

4 Mutate

msleep_brain_to_body <- msleep %>%
  filter(between(sleep_total, 2, 19) & conservation != "domesticated")  %>%
  mutate(brain_to_body = brainwt / bodywt)
datatable(msleep_brain_to_body,options = list(
  pageLength=5, scrollX='400px'))

5 Combinations

msleep_filter <- msleep %>%
  filter(between(sleep_total, 2, 19) & conservation != "domesticated")  %>%
  mutate(brain_to_body = brainwt / bodywt)

msleep_groupVore <- msleep_filter %>%     
  group_by(vore) %>% 
  summarise(mean(brain_to_body))
datatable(msleep_groupVore, options = list(pageLength = 5))

5.1 Two summarise

  • Add a column with the counter for observations for each row with n()
msleep_grouped <- msleep_filter %>%     
  group_by(vore) %>% 
  summarise(mean = mean(brain_to_body), count = n())
datatable(msleep_grouped, options = list(pageLength = 5))

5.2 Sorting

  • Order the information in descending order (MEAN)
msleep_grouped %>%     
  arrange(desc(mean))
## # A tibble: 5 x 3
##   vore        mean count
##   <fct>      <dbl> <int>
## 1 insecti  0.00868     2
## 2 <NA>     0.00510     2
## 3 carni   NA          11
## 4 herbi   NA          18
## 5 omni    NA           8
  • Order the information in descending order (COUNT)
msleep_grouped %>%     
  arrange(desc(count))
## # A tibble: 5 x 3
##   vore        mean count
##   <fct>      <dbl> <int>
## 1 herbi   NA          18
## 2 carni   NA          11
## 3 omni    NA           8
## 4 insecti  0.00868     2
## 5 <NA>     0.00510     2