liftr-tidyverse.Rmd 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ---
  2. title: "Explore tidyverse with liftr"
  3. author: "Nan Xiao <<[email protected]>>"
  4. date: "`r Sys.Date()`"
  5. output:
  6. rmarkdown::pdf_document:
  7. toc: true
  8. number_sections: true
  9. liftr:
  10. from: "rocker/tidyverse:latest"
  11. maintainer: "Nan Xiao"
  12. email: "[email protected]"
  13. pandoc: false
  14. texlive: true
  15. cran:
  16. - nycflights13
  17. ---
  18. \clearpage
  19. # ggplot2
  20. The example is from: https://github.com/tidyverse/ggplot2.
  21. ```{r}
  22. library("ggplot2")
  23. ggplot(mpg, aes(displ, hwy, colour = class)) +
  24. geom_point()
  25. ```
  26. # tibble
  27. The examples are from: https://github.com/tidyverse/tibble.
  28. ```{r}
  29. library("tibble")
  30. as_tibble(iris)
  31. tibble(x = 1:5, y = 1, z = x ^ 2 + y)
  32. tribble(
  33. ~x, ~y, ~z,
  34. "a", 2, 3.6,
  35. "b", 1, 8.5)
  36. ```
  37. # purrr
  38. The example is from: https://github.com/tidyverse/purrr.
  39. ```{r}
  40. library("purrr")
  41. mtcars %>%
  42. split(.$cyl) %>% # from base R
  43. map(~ lm(mpg ~ wt, data = .)) %>%
  44. map(summary) %>%
  45. map_dbl("r.squared")
  46. ```
  47. # dplyr
  48. The examples are from: https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html.
  49. ```{r}
  50. library("dplyr")
  51. library("nycflights13")
  52. filter(flights, month == 1, day == 1)
  53. slice(flights, 1:10)
  54. arrange(flights, year, month, day)
  55. select(flights, year, month, day)
  56. mutate(flights,
  57. gain = arr_delay - dep_delay,
  58. speed = distance / air_time * 60)
  59. summarise(flights,
  60. delay = mean(dep_delay, na.rm = TRUE))
  61. ```
  62. # Session information
  63. The R session information for compiling this document is shown below.
  64. ```{r session}
  65. sessionInfo()
  66. ```