Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

of

第6回 関数とフロー制御 Slide 1 第6回 関数とフロー制御 Slide 2 第6回 関数とフロー制御 Slide 3 第6回 関数とフロー制御 Slide 4 第6回 関数とフロー制御 Slide 5 第6回 関数とフロー制御 Slide 6 第6回 関数とフロー制御 Slide 7 第6回 関数とフロー制御 Slide 8 第6回 関数とフロー制御 Slide 9 第6回 関数とフロー制御 Slide 10
Upcoming SlideShare
What to Upload to SlideShare
Next
Download to read offline and view in fullscreen.

1 Like

Share

Download to read offline

第6回 関数とフロー制御

Download to read offline

西南学院大学経済学部 演習1 講義ノート
講義ページ: http://courses.wshito.com/semi1/2020-datascience/index.html

第6回 関数とフロー制御

  1. 1. 6 2020 3 8 1 1 2 1 2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3 5 3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1.1 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1.2 ifelse() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.1.3 switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3.2.1 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3.2.2 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.4 apply . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1 • • 2 csv read.csv() data.frame() 1
  2. 2. 2.1 6 2.1 function() Hello World! hello() 1 + + > hello <- function() { # + cat("Hello Worldn") + } > hello() # Hello World function() { } function() ( ) name > hello <- function(name) { + cat (" ", name, "n", sep="") + } > hello(" !") ! > add <- function (x, y) { + x + y + } > add(10, 5) [1] 15 add() x y add(10, 5) 10 5 2.2 > calc <- function (x, y) { + x * y + x - y + x + y # calc I 2
  3. 3. 2.3 6 + } > calc (3, 4) [1] 7 return() > calc <- function (x, y) { + x * y + x - y + return(x + y) # + } > calc (1, 2) [1] 3 return() 2 > calc <- function (x, y) { + x * y + return(x - y) # x-y + x + y # + } > calc (1, 2) [1] -1 2.3 hello() name " " hello() name " " > hello <- function(name=" ") { # name + cat (" ", name, "n", sep="") + } > hello() # name > hello(" !") # name ! ! name birthday I 3
  4. 4. 2.4 6 > greeting <- function(name, birthday, greet=" ", to=" ") { + str <- paste(to, " ", greet, " ", + " ", name, " ", + " ", birthday, " ", sep="") + str + } > greeting(" ", "4 1 ") # greet to [1] " 4 1 " str str R str str greeting() > greeting(" ", "4 1 ", " ") # to [1] " 4 1 " > greeting(" ", "4 1 ", " ", " ") [1] " 4 1 " greet 3 greet > greeting(" ", "4 1 ", " ") [1] " 4 1 " > greeting(" ", "4 1 ", to=" ") [1] " 4 1 " > greeting(greet=" ", name=" ", to=" ", birthday="4 1 ") [1] " 4 1 " 2.4 greeting() g g() greeting() I 4
  5. 5. 6 > g <- greeting > g(name=" ", birthday="4 ") [1] " 4 " add() adder() x y add > add <- function(x, y, adder) { + adder(x, y) + } add add.num() add add.str() > add.num <- function(x, y) { + x + y # + } > add.str <- function(x, y) { + paste(x, y) # + } add() > add(1, 5, add.num) [1] 6 > add("a", "b", add.str) [1] "a b" > add("a", "b", function(x, y) {paste(x, y, sep="+")}) [1] "a+b" 3 I 5
  6. 6. 3.1 6 3.1 3.1.1 if TRUE FALSE if if > absolute.value <- function(x) { + if (x > 0) { + x # x > 0 TRUE x + } else { + -x # x > 0 FALSE x + } + } > absolute.value(5) [1] 5 > absolute.value(-9) [1] 9 TRUE return(x) x FALSE else > absolute.value <- function(x) { + if (x > 0) { + return(x) # x > 0 TRUE x + } + -x # x > 0 FALSE x + } > absolute.value(-100) [1] 100 > absolute.value(4) [1] 4 if-else 90 A 80 B 60 C D grade() > grade <- function(score) { + if (score >= 90) { + "A" + } else if (score >= 80) { + "B" + } else if (score >= 60) { + "C" + } else { + "D" + } + } > grade(100) [1] "A" I 6
  7. 7. 3.1 6 > grade(34) [1] "D" > grade(65) [1] "C" > grade(83) [1] "B" grade() else if { } else if 1 1 > grade <- function(score) { + if (score >= 90) { + "A" + } else { + if (score >= 80) { + "B" + } else { + if (score >= 60) { + "C" + } else { + "D" + } + } + } + } if grade() if else 1 > grade <- function(score) { + if (score >= 90) + "A" + else if (score >= 80) + "B" + else if (score >= 60) + "C" + else + "D" + } 3.1.2 ifelse() if-else ifelse() ifelse() ifelse() I 7
  8. 8. 3.2 6 ifelse( , , ) > absolute.value <- function(x) { # ifelse + ifelse(x > 0, x, -x) + } > grade <- function(score) { # ifelse + ifelse(score >= 90, "A", + ifelse(score >= 80, "B", + ifelse(score >= 60, "C", "D"))) + } ifelse() 3.1.3 switch if-else 2 switch() switch( , 1, 2, 3, ...) 1 > switch(3, " 1", " 2", " 3", " 4") # 3 [1] " 3" > switch("b", a=" 1", b=" 2", c=" 3") # b [1] " 2" 3.2 R for while R 3.4 apply 3.2.1 for for for(i in ) i i { } I 8
  9. 9. 3.2 6 > z <- 0 # z 0 > for(i in 1:10) { + z <- z + i # 1 10 + } > z # for z [1] 55 > for(n in c("a", "b", "c", "d")) # + cat(toupper(n), "n") # toupper() A B C D for 2 > for(i in 1:5) { + for(j in 1:5) + cat(i, "-", j, ", ", sep="") + cat("n") + } 1-1, 1-2, 1-3, 1-4, 1-5, 2-1, 2-2, 2-3, 2-4, 2-5, 3-1, 3-2, 3-3, 3-4, 3-5, 4-1, 4-2, 4-3, 4-4, 4-5, 5-1, 5-2, 5-3, 5-4, 5-5, 3.2.2 while while > countdown <- function(n) { # n 0 + while (n != 0) { + cat(n, ", ", sep="") + n <- ifelse (n >= 0, n-1, n+1) # n + } + cat("0n") + } > countdown(10) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 > countdown(-5) -5, -4, -3, -2, -1, 0 I 9
  10. 10. 3.3 6 3.3 (1) (7) 3 GDP ppp GDP compare.gdp() 2010 > compare.gdp(ppp, 2010) Qatar 117518.7 Japan 34986.99 Congo, Dem. Rep. 646.2954 (2) x y gcd(x, y) 3.4 apply for while R apply apply 1 1 2 4 ? 3 2 ? I 10
  • JinseiHamamoto

    Apr. 29, 2020

西南学院大学経済学部 演習1 講義ノート 講義ページ: http://courses.wshito.com/semi1/2020-datascience/index.html

Views

Total views

393

On Slideshare

0

From embeds

0

Number of embeds

1

Actions

Downloads

17

Shares

0

Comments

0

Likes

1

×