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

RBC202003_Day1_Period3

sakaue
March 19, 2020

 RBC202003_Day1_Period3

sakaue

March 19, 2020
Tweet

More Decks by sakaue

Other Decks in Education

Transcript

  1. •> hako <- c(1,2,3,4,5) •> hako • c() ؔ਺ɿcombine (

    cf. https://twitter.com/#!/sakaue/status/193708048030760960 ) • ஋Λ̍ͭʹ·ͱΊΔؔ਺ • ٯʹॻ͍ͯ΋ʢҰԠʣOK 1. ϕΫτϧͱ͸Կ͔
  2. c()ؔ਺ͷ “<-” ͸Կʁ hako <- c(1,2,3,4,5) ͷ “<-” ͸ ࠨ޲͖ͷ໼ҹʢˡʣ

    Λදݱ ʢೖΕସ͑ͯ΋ಈ͖·͢ɻʮ=ʯ΋࢖͑·͢ɻʣ 1. ϕΫτϧͱ͸Կ͔
  3. 1. ϕΫτϧͱ͸Կ͔ • ϕΫτϧͷಛఆͷཁૉΛऔΓग़͢ • 3൪໨ͷཁૉ͚ͩΛऔΓग़͢ • hako[3] • 3

    ͚͕ͩදࣔ͞ΕΔ • 2൪໨͔Β4൪໨ͷཁૉΛऔΓग़͢ • hako[2 : 4] • 2, 3, 4 ͷ3ཁૉ͕දࣔ͞ΕΔ (p. 56)
  4. 1. ϕΫτϧͱ͸Կ͔ • ϕΫτϧΛ࢖ͬͨܭࢉ • ͢΂ͯͷཁૉΛ2ഒ͢Δ • hako * 2

    • ผͷϕΫτϧΛ࡞੒ͦ͠ΕͧΕΛ଍͢ • hako2 <- c(6, 7, 8, 9, 10) • hako + hako2 • ͦΕͧΕͷཁૉಉ͕࢜଍͞ΕΔ • ཁૉ͕͚ܽΔͱΤϥʔ͕ग़Δ (p. 56)
  5. 1. ϕΫτϧͱ͸Կ͔ • ϕΫτϧ͸ෳ਺ͷ஋Λ·ͱΊͨ΋ͷ • σʔλΛ݁߹͢Δ • vector.1 <- append(hako,

    hako2) • vector.1 ͱೖྗ͠த਎Λ֬ೝ • vector.2 <- append(hako2, hako) • vector.2 ͱೖྗ͠த਎Λ֬ೝ • ࢦఆͨ͠ॱং௨Γʹ݁߹͞ΕΔ (p. 56)
  6. දʹ͢Ε͹... ਓ ਎௕ ମॏ A 180 75 B 170 65

    C 165 60 D 175 70 E 190 80 2. ߦྻͱ͸Կ͔
  7. ߦ

  8. • ϕΫτϧΛ࡞੒͔ͯ͠Βߦྻʹม׵ Part 1 • hako3 <- c(1, 2, 3,

    4, 5, 6, 7, 8, 9) • matrix.1 <- matrix(hako3, nrow=3, ncol=3) • Ҿ਺ʢargumentʣͱͯ͠ߦྻ਺Λࢦఆ • nrow: ߦ਺Λࢦఆɼncol: ྻ਺Λࢦఆ • matrix.1 ͚ͩΛೖྗͯ͠த਎Λ֬ೝ 2. ߦྻͱ͸Կ͔
  9. • ϕΫτϧΛ࡞੒͔ͯ͠Βߦྻʹม׵ Part 2 • matrix.2 <- matrix(hako3, nrow=3, ncol=3,

    byrow= TRUE) • byrow = TRUE ʹΑΓԣํ޲΁ཁૉΛ഑ஔ ɹ • nrow: ߦ਺Λࢦఆɼncol: ྻ਺Λࢦఆ • matrix.2 ͚ͩΛೖྗͯ͠த਎Λ֬ೝ 2. ߦྻͱ͸Կ͔
  10. 2. ߦྻͱ͸Կ͔ • ߦྻΛ࢖ͬͨܭࢉ • matrix.2 + 1 #֤ཁૉʹ1Λ଍͢ •

    ผͷߦྻΛ࡞੒ͦ͠ΕͧΕΛ଍͢ • matrix.3 <- matrix(c(10:18), nrow=3, ncol=3, byrow=TRUE) • matrix.2 + matrix.3 • 9ͭͷཁૉ͕଍͞Ε͍ͯΔ͔֬ೝ
  11. 2. ߦྻͱ͸Կ͔ • ߦྻͷ݁߹ • rbind() ؔ਺: ߦํ޲ʢԼʣʹߦྻΛ݁߹ • rbind(matrix.2,

    matrix.3) • cbind() ؔ਺: ྻํ޲ʢӈʣʹߦྻΛ݁߹ • cbind(matrix.2, matrix.3)
  12. 2. ߦྻͱ͸Կ͔ • ߦྻͷཁૉΛऔΓग़͢ • matrix.2[2, 3] #2ߦ໨ͷ3ྻ໨ʹ͋Δཁૉ • matrix.2[2,

    ] #2ߦ໨ͷཁૉ͢΂ͯ • matrix.2[, 3] #3ྻ໨ͷཁૉ͢΂ͯ • matrix.2[-2, ] #2ߦ໨<Ҏ֎>ͷཁૉ͢΂ͯ • matrix.2[, -3] #3ྻ໨<Ҏ֎>ͷཁૉ͢΂ͯ
  13. 2. ߦྻͱ͸Կ͔ • ߦྻʹϥϕϧʢ໊લʣΛ͚ͭΔ • rownames(matrix.2) <- c("R1", "R2", "R3")

    • ߦϥϕϧͷ෇༩ • colnames(matrix.2) <- c("C1", "C2", "C3") • ྻϥϕϧͷ෇༩ • matrix.2 Λೖྗ݁͠ՌΛ֬ೝ
  14. 3. ੔વσʔλͱ͸Կ͔ • R քͷਆɼHadley Wickham ࢯఏএͷ "Tidy Data" •

    ࿦จ: http://vita.had.co.nz/papers/tidy-data.html • ࢀߟ: http://id.fnshr.info/2017/01/09/tidy-data-intro/ • ʮ1ྻʹʢॎํ޲ʣ1ม਺ʯͷܗࣜʹ͢Δ͜ͱ • ෼ੳ༻ͷσʔλܗࣜ͸͜Ε͕େݪଇ • ม਺Λԣʢߦʣํ޲ʹฒ΂ͨΓ͸͠ͳ͍ • Excel Ͱηϧͷ݁߹ͳΜͧ͠ΑͬͨΒ...ʢౖʣ
  15. ೥౓ʘ݄ 4݄ 5݄ 6݄ H30 124 183 241 H31 205

    367 307 R01 582 759 998 3. ੔વσʔλͱ͸Կ͔ • Α͘ݟ͔͚ΔλΠϓͷද • ਓʹݟͤʢͯղऍ͢ʣΔදͱͯ͠͸ OK • σʔλ෼ੳ༻ͷදͱͯ͠͸ NG • ॎͱԣʹม਺͕ަࠩͨ͠ঢ়ଶ͔ͩΒ
  16. ೥౓ ݄ ΞΫηε਺ H30 4 124 H30 5 183 H30

    6 241 H31 4 205 H31 5 367 H31 6 307 R01 4 582 R01 5 759 R01 6 998 3. ੔વσʔλͱ͸Կ͔ • ෼ੳ༻ʹ͸ʮ1ྻʹʢॎํ޲ʣ1ม਺ʯ • 1ߦʢԣํ޲ʣʹ1έʔεɾ1Ϩίʔυ • ݄Λԣʢߦʣํ޲ʹฒ΂ͨΓ͸͠ͳ͍
  17. • ࢝Ί͔Β੔વσʔλʹͳ͍ͬͯΔ͜ͱ͸গͳ͍(?) • ͦ͏ͨ͠σʔλΛมܗɾ੔ཧ͢ΔͨΊʹɼR Ͱ͸ "tidyverse" ͱ͍͏ύοέʔδ͕ར༻Մೳ • tidyverse ʹؚ·ΕΔύοέʔδΛ·ͱΊͯΠϯ

    ετʔϧ͢ΔͨΊͷύοέʔδ • ggplot2: άϥϑඳը • dplyr: σʔλૢ࡞ʢ৚݅நग़ɼྻ௥ՃͳͲʣ • tidyr: ੔વσʔλ࡞੒ • ͦͷଞଟ਺ͷύοέʔδ͋Γ 3. ੔વσʔλͱ͸Կ͔
  18. • ຊߨशձͰ͸ "Tidy Data" ͷઆ໌ͱɼ"tidyverse" ύοέʔδͷ঺հͷΈʢૢ࡞͕Ұ෦ಛघͳͨΊʣ • େྔͷσʔλΛ੔ܗ͢Δࡍɼ΄΅ඞਢͷύο έʔδͱͳΓͭͭ͋Δ •

    ࢀߟ1: https://r4ds.had.co.nz/ (R for Data Science) • ࢀߟ2: https://moderndive.com/index.html ɹɹɹɹɹɹ (A moderndive into R and the tidyverse) • େࣄͳ͜ͱ͸ɼʮݟͯղऍ͢ΔදʯͱʮσʔλΛ อଘ͢ΔදʯʢʹTidy DataʣΛ۠ผͯ͠อଘͯ͠ ͓͘͜ͱ 3. ੔વσʔλͱ͸Կ͔
  19. 2. 1͔Β50·Ͱͷ਺஋Λɼ10ߦ5ྻͷߦྻʹม׵ 3. 2 Ͱ࡞੒ͨ͠ߦྻͷ7ߦ໨ͷཁૉΛऔΓग़͢ 4. 3 ͰऔΓग़ͨ͠7ߦ໨ͷཁૉͷ߹ܭ஋Λࢉग़͢Δʢ1ߦͰʣ 5. 2

    Ͱ࡞੒ͨ͠ߦྻͷ3ྻ໨ͷཁૉΛऔΓग़͢ 6. 2 Ͱ࡞੒ͨ͠ߦྻͷ5ߦ໨ʻҎ֎ʼͷཁૉΛऔΓग़͢ 7. 2 Ͱ࡞੒ͨ͠ߦྻͷ2ߦ໨ͱ7ߦ໨ͷཁૉΛಉ࣌ʹऔΓग़͢ 8. 2 Ͱ࡞੒ͨ͠ߦྻͷ2ྻ໨ͱ4ྻ໨ͷཁૉΛಉ࣌ʹऔΓग़͢ 9. 2 Ͱ࡞੒ͨ͠ߦྻͷ2ྻ໨ͱ4ྻ໨ͷཁૉͷฏۉ஋Λࢉग़͢Δ 10. 2 Ͱ࡞੒ͨ͠ߦྻʹϥϕϧΛ͚ͭΔʢR1 … R10, C1 … C5ʣ 4. ԋशʹ͙࣍ԋश
  20. 2. matrix() ؔ਺ɼҾ਺ͷ nrow / ncol, byrow ʹ΋༻৺ 3. ΧοίͷछྨͱΧϯϚͷҐஔʹ஫ҙ

    4. ߹ܭ஋ΛٻΊΔʹ͸ɼS** ؔ਺ 5. ΧοίͷछྨͱΧϯϚͷҐஔʹ஫ҙ 6. ʮҎ֎ʯ͸ɼϋΠϑϯͰࢦఆ 7. ಉ࣌ʹࢦఆ͢Δͱ͖͸ɼc() ؔ਺Λ૊Έ߹Θ࣮ͤͯߦ 8. ಉ࣌ʹࢦఆ͢Δͱ͖͸ɼc() ؔ਺Λ૊Έ߹Θ࣮ͤͯߦ 9. ฏۉ஋ΛٻΊΔʹ͸ɼm*** ؔ਺ 10. rownames/colnames Ͱɼจࣈྻʹ͸ೋॏҾ༻ූΛ௥ه 4. ԋशʹ͙࣍ԋशʢώϯτʣ