"DC Intermediate R - Loops"의 두 판 사이의 차이

14번째 줄: 14번째 줄:
|}
|}


==While loop==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=1
<source lang='r'>
</source>
==Write a while loop==
==Write a while loop==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=2
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=2
<source lang='r'>
<source lang='r'>
# Initialize the speed variable
speed <- 64
# Code the while loop
while (speed>30) {
  print("Slow down!")
  speed <- speed - 7
}
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
# Print out the speed variable
speed
## [1] 29
</source>
</source>
==Throw in more conditionals==
==Throw in more conditionals==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=3
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=3
<source lang='r'>
<source lang='r'>
# Initialize the speed variable
speed <- 64
# Extend/adapt the while loop
while (speed > 30) {
  print(paste("Your speed is",speed))
  if (speed > 48) {
    print("Slow down big time!")
    speed <- speed - 11
  } else {
    print("Slow down!")
    speed <- speed - 6
  }
}
## [1] "Your speed is 64"
## [1] "Slow down big time!"
## [1] "Your speed is 53"
## [1] "Slow down big time!"
## [1] "Your speed is 42"
## [1] "Slow down!"
## [1] "Your speed is 36"
## [1] "Slow down!"
</source>
</source>
==Stop the while loop: break==
==Stop the while loop: break==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=4
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=4
<source lang='r'>
<source lang='r'>
# Initialize the speed variable
speed <- 88
while (speed > 30) {
  print(paste("Your speed is", speed))
 
  # Break the while loop when speed exceeds 80
  if (speed > 80) {
    break
  }
  if (speed > 48) {
    print("Slow down big time!")
    speed <- speed - 11
  } else {
    print("Slow down!")
    speed <- speed - 6
  }
}
## [1] "Your speed is 88"
</source>
</source>
==Build a while loop from scratch==
==Build a while loop from scratch==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=5
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=5
<source lang='r'>
<source lang='r'>
# Initialize i as 1
i <- 1
# Code the while loop
while (i <= 10) {
  print(3 * i)
  if ( (3 * i) %% 8 == 0 ) {
    print(3 * i)
    break
  }
  i <- i + 1
}
## [1] 3
## [1] 6
## [1] 9
## [1] 12
## [1] 15
## [1] 18
## [1] 21
## [1] 24
## [1] 24
</source>
</source>
==For loop==
 
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=6
<source lang='r'>
</source>
==Loop over a vector==
==Loop over a vector==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=7
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=7
<source lang='r'>
<source lang='r'>
# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
# Loop version 1
for (p in linkedin) {
  print(p)
}
## [1] 16
## [1] 9
## [1] 13
## [1] 5
## [1] 2
## [1] 17
## [1] 14
# Loop version 2
for (i in 1:length(linkedin)) {
  print(linkedin[i])
}
## [1] 16
## [1] 9
## [1] 13
## [1] 5
## [1] 2
## [1] 17
## [1] 14
</source>
</source>
==Loop over a list==
==Loop over a list==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=8
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=8
<source lang='r'>
<source lang='r'>
# The nyc list is already specified
nyc <- list(pop = 8405837,
            boroughs = c("Manhattan", "Bronx", "Brooklyn", "Queens", "Staten Island"),
            capital = FALSE)
# Loop version 1
for (p in nyc) {
  print(p)
}
## [1] 8405837
## [1] "Manhattan"    "Bronx"        "Brooklyn"      "Queens"     
## [5] "Staten Island"
## [1] FALSE
# Loop version 2
for (i in 1:length(nyc)) {
  print(nyc[[i]])
}
## [1] 8405837
## [1] "Manhattan"    "Bronx"        "Brooklyn"      "Queens"     
## [5] "Staten Island"
## [1] FALSE
</source>
</source>
==Loop over a matrix==
==Loop over a matrix==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=9
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=9
<source lang='r'>
<source lang='r'>
# The tic-tac-toe matrix ttt has already been defined for you
ttt <- matrix(c("O", NA, "X", NA, "O", "O", "X", NA, "X"), ncol = 3, nrow = 3, byrow = TRUE)
# define the double for loop
for (i in 1:nrow(ttt)) {
  for (j in 1:ncol(ttt)) {
    print(paste("On row", i, "and column", j, "the board contains", ttt[i, j]))
  }
}
## [1] "On row 1 and column 1 the board contains O"
## [1] "On row 1 and column 2 the board contains NA"
## [1] "On row 1 and column 3 the board contains X"
## [1] "On row 2 and column 1 the board contains NA"
## [1] "On row 2 and column 2 the board contains O"
## [1] "On row 2 and column 3 the board contains O"
## [1] "On row 3 and column 1 the board contains X"
## [1] "On row 3 and column 2 the board contains NA"
## [1] "On row 3 and column 3 the board contains X"
</source>
</source>
==Mix it up with control flow==
==Mix it up with control flow==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=10
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=10
<source lang='r'>
<source lang='r'>
# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
# Code the for loop with conditionals
for (li in linkedin) {
  if (li > 10) {
    print("You're popular!")
  } else {
    print("Be more visible!")
  }
  print(li)
}
## [1] "You're popular!"
## [1] 16
## [1] "Be more visible!"
## [1] 9
## [1] "You're popular!"
## [1] 13
## [1] "Be more visible!"
## [1] 5
## [1] "Be more visible!"
## [1] 2
## [1] "You're popular!"
## [1] 17
## [1] "You're popular!"
## [1] 14
</source>
</source>
==Next, you break it==
==Next, you break it==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=11
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=11
<source lang='r'>
<source lang='r'>
# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
# Extend the for loop
for (li in linkedin) {
  if (li > 10) {
    print("You're popular!")
  } else {
    print("Be more visible!")
  }
 
  # Add if statement with break
  if (li > 16) {
    print("This is ridiculous, I'm outta here!")
    break
  }
 
  # Add if statement with next
  if (li < 5) {
    print("This is too embarrassing!")
    next
  }
 
  print(li)
}
## [1] "You're popular!"
## [1] 16
## [1] "Be more visible!"
## [1] 9
## [1] "You're popular!"
## [1] 13
## [1] "Be more visible!"
## [1] 5
## [1] "Be more visible!"
## [1] "This is too embarrassing!"
## [1] "You're popular!"
## [1] "This is ridiculous, I'm outta here!"
</source>
</source>
==Build a for loop from scratch==
==Build a for loop from scratch==
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=12
* https://campus.datacamp.com/courses/intermediate-r/chapter-2-loops?ex=12
<source lang='r'>
<source lang='r'>
# Pre-defined variables
rquote <- "r's internals are irrefutably intriguing"
chars <- strsplit(rquote, split = "")[[1]]
# Initialize rcount
rcount <- 0
# Finish the for loop
for (char in chars) {
  if (char == "r") rcount <- rcount + 1
  if (char == "u") break
}
# Print out rcount
rcount
## [1] 5
</source>
</source>

2019년 4월 6일 (토) 09:54 판

# DC Intermediate R
DC Intermediate R - Conditionals and Control Flow
DC Intermediate R - Loops
DC Intermediate R - Functions
DC Intermediate R - The apply family
DC Intermediate R - Utilities

1 Write a while loop

# Initialize the speed variable
speed <- 64

# Code the while loop
while (speed>30) {
  print("Slow down!")
  speed <- speed - 7
}
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"
## [1] "Slow down!"

# Print out the speed variable
speed
## [1] 29

2 Throw in more conditionals

# Initialize the speed variable
speed <- 64

# Extend/adapt the while loop
while (speed > 30) {
  print(paste("Your speed is",speed))
  if (speed > 48) {
    print("Slow down big time!")
    speed <- speed - 11
  } else {
    print("Slow down!")
    speed <- speed - 6
  }
}
## [1] "Your speed is 64"
## [1] "Slow down big time!"
## [1] "Your speed is 53"
## [1] "Slow down big time!"
## [1] "Your speed is 42"
## [1] "Slow down!"
## [1] "Your speed is 36"
## [1] "Slow down!"

3 Stop the while loop: break

# Initialize the speed variable
speed <- 88

while (speed > 30) {
  print(paste("Your speed is", speed))
  
  # Break the while loop when speed exceeds 80
  if (speed > 80) {
    break
  }
  if (speed > 48) {
    print("Slow down big time!")
    speed <- speed - 11
  } else {
    print("Slow down!")
    speed <- speed - 6
  }
}
## [1] "Your speed is 88"

4 Build a while loop from scratch

# Initialize i as 1 
i <- 1

# Code the while loop
while (i <= 10) {
  print(3 * i)
  if ( (3 * i) %% 8 == 0 ) {
    print(3 * i)
    break
  }
  i <- i + 1
}
## [1] 3
## [1] 6
## [1] 9
## [1] 12
## [1] 15
## [1] 18
## [1] 21
## [1] 24
## [1] 24

5 Loop over a vector

# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)

# Loop version 1
for (p in linkedin) {
  print(p)
}
## [1] 16
## [1] 9
## [1] 13
## [1] 5
## [1] 2
## [1] 17
## [1] 14

# Loop version 2
for (i in 1:length(linkedin)) {
  print(linkedin[i])
}
## [1] 16
## [1] 9
## [1] 13
## [1] 5
## [1] 2
## [1] 17
## [1] 14

6 Loop over a list

# The nyc list is already specified
nyc <- list(pop = 8405837, 
            boroughs = c("Manhattan", "Bronx", "Brooklyn", "Queens", "Staten Island"), 
            capital = FALSE)

# Loop version 1
for (p in nyc) {
  print(p)
}
## [1] 8405837
## [1] "Manhattan"     "Bronx"         "Brooklyn"      "Queens"       
## [5] "Staten Island"
## [1] FALSE

# Loop version 2
for (i in 1:length(nyc)) {
  print(nyc[[i]])
}
## [1] 8405837
## [1] "Manhattan"     "Bronx"         "Brooklyn"      "Queens"       
## [5] "Staten Island"
## [1] FALSE

7 Loop over a matrix

# The tic-tac-toe matrix ttt has already been defined for you
ttt <- matrix(c("O", NA, "X", NA, "O", "O", "X", NA, "X"), ncol = 3, nrow = 3, byrow = TRUE)

# define the double for loop
for (i in 1:nrow(ttt)) {
  for (j in 1:ncol(ttt)) {
    print(paste("On row", i, "and column", j, "the board contains", ttt[i, j]))
  }
}
## [1] "On row 1 and column 1 the board contains O"
## [1] "On row 1 and column 2 the board contains NA"
## [1] "On row 1 and column 3 the board contains X"
## [1] "On row 2 and column 1 the board contains NA"
## [1] "On row 2 and column 2 the board contains O"
## [1] "On row 2 and column 3 the board contains O"
## [1] "On row 3 and column 1 the board contains X"
## [1] "On row 3 and column 2 the board contains NA"
## [1] "On row 3 and column 3 the board contains X"

8 Mix it up with control flow

# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)

# Code the for loop with conditionals
for (li in linkedin) {
  if (li > 10) {
    print("You're popular!")
  } else {
    print("Be more visible!")
  }
  print(li)
}
## [1] "You're popular!"
## [1] 16
## [1] "Be more visible!"
## [1] 9
## [1] "You're popular!"
## [1] 13
## [1] "Be more visible!"
## [1] 5
## [1] "Be more visible!"
## [1] 2
## [1] "You're popular!"
## [1] 17
## [1] "You're popular!"
## [1] 14

9 Next, you break it

# The linkedin vector has already been defined for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)

# Extend the for loop
for (li in linkedin) {
  if (li > 10) {
    print("You're popular!")
  } else {
    print("Be more visible!")
  }
  
  # Add if statement with break
  if (li > 16) {
    print("This is ridiculous, I'm outta here!")
    break
  }
  
  # Add if statement with next
  if (li < 5) {
    print("This is too embarrassing!")
    next
  }
  
  print(li)
}
## [1] "You're popular!"
## [1] 16
## [1] "Be more visible!"
## [1] 9
## [1] "You're popular!"
## [1] 13
## [1] "Be more visible!"
## [1] 5
## [1] "Be more visible!"
## [1] "This is too embarrassing!"
## [1] "You're popular!"
## [1] "This is ridiculous, I'm outta here!"

10 Build a for loop from scratch

# Pre-defined variables
rquote <- "r's internals are irrefutably intriguing"
chars <- strsplit(rquote, split = "")[[1]]

# Initialize rcount
rcount <- 0

# Finish the for loop
for (char in chars) {
  if (char == "r") rcount <- rcount + 1
  if (char == "u") break
}

# Print out rcount
rcount
## [1] 5
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}