Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SendaiR#1-BeginneRSession

 SendaiR#1-BeginneRSession

Sendai.R#1の初心者セッションで喋ったスライドです。

kilometer

May 18, 2019
Tweet

More Decks by kilometer

Other Decks in Programming

Transcript

  1. 2018.04.21 Tokyo.R #69 BeginneR Session – Data import / Export

    2018.06.09 Tokyo.R #70 BeginneR Session – Bayesian Modeling 2018.07.15 Tokyo.R #71 Landscape with R – the Japanese R community 2018.10.20 Tokyo.R #73 BeginneR Session – Visualization & Plot 2019.01.19 Tokyo.R #75 BeginneR Session – Data pipeline 2019.03.02 Tokyo.R #76 BeginneR Session – Data pipeline 2019.04.13 Tokyo.R #77 BeginneR Session – Data analysis
  2. BeginneR Advanced Hoxo_m If I have seen further it is

    by standing on the shoulders of Giants. -- Sir Isaac Newton, 1676
  3. The tidyverse style guide https://style.tidyverse.org/ "Good coding style is like

    correct punctuation: you can manage without it, butitsuremakesthingseasiertoread." Google's R Style Guide https://style.tidyverse.org/ "The goal of the R Programming Style Guide is to make our R code easier to read, share, and verify." R coding style guides
  4. The tidyverse style guide https://style.tidyverse.org/ "Good coding style is like

    correct punctuation: you can manage without it, butitsuremakesthingseasiertoread." Google's R Style Guide https://style.tidyverse.org/ "The goal of the R Programming Style Guide is to make our R code easier to read, share, and verify." R coding style guides
  5. ブール演算⼦ Boolean Algebra A == B A != B A

    | B A & B A %in% B # equal to # not equal to # or # and # is A in B?
  6. George Boole 1815 - 1864 A Class-Room Introduction to Logic

    https://niyamaklogic.wordpress.co m/category/laws-of-thoughts/ Mathematician Philosopher &
  7. 1JQFBMHFCSB X %>% f X %>% f(y) X %>% f

    %>% g X %>% f(y, .) f(X) f(X, y) g(f(X)) f(y, X) %>% {magrittr} 「dplyr再⼊⾨(基本編)」yutanihilation https://speakerdeck.com/yutannihilation/dplyrzai-ru-men-ji-ben-bian
  8. ① lift Bring milk from the kitchen! lift(Robot, glass, table)

    -> Robot' take ② take(Robot', fridge, milk) -> Robot''
  9. Bring milk from the kitchen! Robot' <- lift(Robot, glass, table)

    Robot'' <- take(Robot', fridge, milk) Robot''' <- pour(Robot'', milk, glass) result <- put(Robot''', glass, table) result <- Robot %>% lift(glass, table) %>% take(fridge, milk) %>% pour(milk, glass) %>% put(glass, table) by using pipe, # ① # ② # ③ # ④ # ① # ② # ③ # ④
  10. {magrittr} # こう書きますか? 536&*/ dat1 <- f1(dat0, var1-1, var1-2) dat2

    <- f2(dat1, var2) dat3 <- f3(dat2, var3) dat4 <- f4(var4-1, dat3, var4-2) dat5 <- f5(dat4, var5) dat6 <- f6(dat5, var6) 536&065 5IJOLJOH 3FBEJOH 1JQFBMHFCSB %>%
  11. {magrittr} # こうやって書く事もできます。 dat <- dat0 %>% f1(var1-1, var1-2) %>%

    f2(var2) %>% f3(var3) %>% f4(var4-1, ., var4-2) %>% f5(var5) %>% f6(var6) */ 065 1JQFBMHFCSB %>%
  12. vector in R in Excel pre <- c(1, 2, 3,

    4, 5) post <- pre * 5 > pre [1] 1 2 3 4 5 > post [1] 5 10 15 20 25
  13. It (dplyr) provides simple “verbs” to help you translate your

    thoughts into code. functions that correspond to the most common data manipulation tasks Introduction to dplyr https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html WFSCT {dplyr}
  14. mutate select filter arrange summaries # add column # select

    column # select row # arrange row # summary of vars {dplyr} WFSCT WFSCGVODUJPOT
  15. > df1 A B 1 1 11 2 2 12

    3 3 13 df1 <- data.frame(A = 1:3, B = 11:13) WFSCT {dplyr} mutate # カラムの追加 > df2 A B C 1 1 11 11 2 2 12 24 3 3 13 39 df2 <- df1 %>% mutate(C = A * B)
  16. library(dplyr) iris %>% select(Sepal.Length, Sepal.Width) 'data.frame': 150 obs. of 6

    variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 ... WFSCT {dplyr}
  17. library(dplyr) iris %>% select(contains(“Width”)) 'data.frame': 150 obs. of 6 variables:

    $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 ... WFSCT {dplyr} Select help functions
  18. WFSCT {dplyr} # Select help functions starts_with("s") ends_with("s") contains("se") matches("^.e")

    one_of(c("Sepal.Length", "Species")) everything() https://kazutan.github.io/blog/2017/04/dplyr-select-memo/ 「dplyr::selectの活⽤例メモ」kazutan
  19. library(dplyr) iris %>% filter(Species == "versicolor") WFSCT {dplyr} 'data.frame': 50

    obs. of 5 variables: $ Sepal.Length: num 7 6.4 6.9 5.5 6.5 5.7 6.3 ... $ Sepal.Width : num 3.2 3.2 3.1 2.3 2.8 2.8 ... $ Petal.Length: num 4.7 4.5 4.9 4 4.6 4.5 4.7 ... $ Petal.Width : num 1.4 1.5 1.5 1.3 1.5 1.3 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 2 2 ...
  20. library(dplyr) iris %>% filter(Species == "versicolor") WFSCT {dplyr} NSE (Non-Standard

    Evaluation) 'data.frame': 50 obs. of 5 variables: $ Sepal.Length: num 7 6.4 6.9 5.5 6.5 5.7 6.3 ... $ Sepal.Width : num 3.2 3.2 3.1 2.3 2.8 2.8 ... $ Petal.Length: num 4.7 4.5 4.9 4 4.6 4.5 4.7 ... $ Petal.Width : num 1.4 1.5 1.5 1.3 1.5 1.3 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 2 2 ...
  21. mutate select filter arrange summaries # カラムの追加 # カラムの選択 #

    ⾏の絞り込み # ⾏の並び替え # 値の集約 {dplyr} WFSCT WFSCؔ਺܈
  22. (SBNNBSPGEBUBNBOJQVMBUJPO By constraining your options, it helps you think about

    your data manipulation challenges. Introduction to dplyr https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html
  23. より多くの制約を課す事で、 魂の⾜枷から、より⾃由になる。 Igor Stravinsky И ́ горь Ф Страви́нский The

    more constraints one imposes, the more one frees one's self of the chains that shackle the spirit. 1882 - 1971 ※ 割と意訳
  24. Text Image First, A. Next, B. Then C. Finally D.

    time Intention encode "Frozen" structure A B C D time value α β
  25. Basic plotting functions in {graphics} ‧High-level: create a new plot

    ‧Low-level: add more information ‧Interactive: interactively add info.
  26. Basic plotting functions in {graphics} ‧High-level: create a new plot

    ‧Low-level: add more information ‧Interactive: interactively add info.
  27. ‧High-level: create a new plot Basic plotting functions in {graphics}

    x <- c(1:10) y <- 0.5 * x + 3 plot(x, y) Scatterplot of two vectors
  28. ‧High-level: create a new plot x <- c(1:10) y <-

    0.5 * x + 3 plot(x, y, type = "b") Scatterplot of two vectors Basic plotting functions in {graphics}
  29. ‧High-level: create a new plot x <- c(1:10) y <-

    0.5 * x + 3 plot(x, y, type = "o") Scatterplot of two vectors Basic plotting functions in {graphics}
  30. ‧High-level: create a new plot x <- c(1:10) y <-

    0.5 * x + 3 plot(x, y, type = "o", col = "red") Scatterplot of two vectors Basic plotting functions in {graphics}
  31. ‧High-level: create a new plot x <- c(1:10) y <-

    0.5 * x + 3 plot(x, y, type = "o", col = "red", pch = 2) Scatterplot of two vectors Basic plotting functions in {graphics}
  32. ‧High-level: create a new plot Scatterplot of two vectors R-Tips,

    http://cse.naro.affrc.go.jp/takezawa/r-tips/r/53.html Basic plotting functions in {graphics}
  33. ‧High-level: create a new plot x <- c(1:10) y <-

    0.5 * x + 3 plot(x, y, type = "o", col = "red", pch = 2, lwd = 5) Scatterplot of two vectors Basic plotting functions in {graphics}
  34. How to plot multiple data series + x <- 1:10

    y <- 0.5 * x + 3 x <- 1:10 y2 <- 0.25 * x + 0.1 * x^2 + 1 Basic plotting functions in {graphics}
  35. How to plot multiple data series x <- c(1:10) y1

    <- 0.5 * x + 3 y2 <- 0.25 * x + 0.1 * x^2 + 1 # create & set a new plot: high level plot(0, 0, type = "n", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2)), xlab = "x", ylab = "y") # add elements: low level funcs. lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2) Basic plotting functions in {graphics}
  36. How to plot multiple data series x <- c(1:10) y1

    <- 0.5 * x + 3 y2 <- 0.25 * x + 0.1 * x^2 + 1 # create & set a new plot: high level plot(0, 0, type = "n", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2)), xlab = "x", ylab = "y") # add elements: low level funcs lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2) Basic plotting functions in {graphics}
  37. # set graphic params: par par(mar = c(3, 3, 0.5,

    0.5), tcl = -0.2, mgp = c(1.5, 0.3, 0), bty = "l") # create & set a new plot: high level plot(0, 0, type = "n", xlab = "x", ylab = "y", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2))) # add elements: low level funcs. lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2) Basic plotting functions in {graphics}
  38. "declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔

    ふつ〜に思ったことを書けば描けるというニュアンス (直訳すると"平叙⽂の"ぐらいの意味)
  39. "declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔

    ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. https://ggplot2.tidyverse.org/index.html ふつ〜に思ったことを書けば描ける
  40. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_point()+ scale_... theme_... in {graphics} in {ggplot2}
  41. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_point()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame
  42. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame ① data ② encode ③ style (optional)
  43. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_point() factor class "declarative" graphic package {ggplot2}
  44. Sample data iris %>% str row column 'data.frame': 150 obs.

    of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 ... $ Species : Factor w/ 3 levels "setosa", ... class names mode value
  45. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Petal.Length))+

    geom_point() continuous volume "declarative" graphic package {ggplot2}
  46. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_line() "declarative" graphic package {ggplot2}
  47. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_smooth() "declarative" graphic package {ggplot2}
  48. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_smooth()+ geom_point() "declarative" graphic package {ggplot2}
  49. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_smooth()+ geom_point(aes(shape=Species), size = 4, alpha = 0.5) "declarative" graphic package {ggplot2}
  50. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_smooth()+ geom_point()+ scale_color_manual(values = c("Blue", "Red", "Black")) "declarative" graphic package {ggplot2}
  51. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_smooth()+ geom_point()+ theme_classic()+ "declarative" graphic package {ggplot2}
  52. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_smooth()+ geom_points()+ theme_classic()+ theme(legend.position = "bottom") "declarative" graphic package {ggplot2}
  53. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame ① data ② encode ③ style (optional)
  54. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} # open graphic device png("img.png", ...) # colse graphic device dev.off() ggsave("img.png", width = 5, height = 4)
  55. # set graphic params par(...) # set a new plot

    plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} # open graphic device png("img.png", ...) # colse graphic device dev.off() ggsave("img.png", width = 5, height = 4) 1 2 3 4 5 1 2 3 4 5
  56. "declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔

    ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. https://ggplot2.tidyverse.org/index.html ふつ〜に思ったことを書けば描ける
  57. Social communication output input/feedback decode encode internal status A B

    estimated B status A status Emotion, Intention, ... = Language, Gesture, Prosody, Face expression...
  58. BeginneR Advanced Hoxo_m If I have seen further it is

    by standing on the sholders of Giants. -- Sir Isaac Newton, 1676