#classroom
channel in Slack for link06:00
It's nice to see your faces :)
If you're okay with it, please turn on your camera - it creates a more engaging discussion environment and an opportunity for us to get to know each other better. Your privacy is important though, and I understand if you wish to keep it off. No pressure.
functionName <- function(arguments) { # Do stuff here return(something)}
In English:
"
functionName
is afunction
ofarguments
that does..."
functionName <- function(arguments) { # Do stuff here return(something)}
Example:
"
squareRoot
is afunction
ofn
that...returns the square root ofn
"
squareRoot <- function(n) { return(n^0.5)}
Example:
"
squareRoot
is afunction
ofn
that...returns the square root ofn
"
squareRoot <- function(n) { return(n^0.5)}
squareRoot(64)
## [1] 8
return()
and cat()
statementsreturn()
and cat()
statementsisPositive <- function(n) { return(n > 0)}
return()
and cat()
statementsisPositive <- function(n) { return(n > 0)}
isPositive <- function(n) { cat(n > 0)}
return()
and cat()
statementsisPositive <- function(n) { return(n > 0)}
return()
returns back a value
test <- isPositive(7)test
TRUE
isPositive <- function(n) { cat(n > 0)}
cat()
prints a value as a string
test <- isPositive(7)
TRUE
test
Error: object 'test' not found
cat()
is short for "concatenating"cat()
is short for "concatenating"print_x <- function(x) { cat("The value of x is", x)}
cat()
is short for "concatenating"print_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
cat()
is short for "concatenating"print_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
print_x_squared <- function(x) { cat("The value of x is", x, "and the value of x^2 is", x^2)}
cat()
is short for "concatenating"print_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
print_x_squared <- function(x) { cat("The value of x is", x, "and the value of x^2 is", x^2)}
print_x_squared(7)
## The value of x is 7 and the value of x^2 is 49
cat()
adds a space between values by defaultcat()
adds a space between values by defaultprint_x <- function(x) { cat("The value of x is", x)}
cat()
adds a space between values by defaultprint_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
cat()
adds a space between values by defaultprint_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
Modify separator with the sep
argument:
print_x <- function(x) { cat("The value of x is", x, sep = ": ")}
cat()
adds a space between values by defaultprint_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
Modify separator with the sep
argument:
print_x <- function(x) { cat("The value of x is", x, sep = ": ")}
print_x(7)
## The value of x is: 7
02:00
Consider these functions:
f1 <- function(x) { return(x^3)}f2 <- function(x) { cat(x^3)}f3 <- function(x) { cat(x^3) return(x^4)}f4 <- function(x) { return(x^3) cat(x^4)}
What will these lines of code produce?
Write your answer down first, then run the code to check.
f1(2)f2(2)f3(2)f4(2)
squareOfX <- function(x) { y <- x^2 # y here is "local" return(y)}
squareOfX <- function(x) { y <- x^2 # y here is "local" return(y)}
squareOfX(3)
## [1] 9
If you try to call y
, you'll get an error:
y
Error: object 'y' not found
print_x <- function(x) { cat(x) cat(n) # n is global!}n <- 5 # Define n in the *global* environmentprint_x(5)
## 55
print_x <- function(x) { cat(x) cat(n) # n is global!}n <- 5 # Define n in the *global* environmentprint_x(5)
## 55
n <- 6print_x(5)
## 56
Function behavior shouldn't change with the same arguments!
print_x <- function(x, n = NULL) { cat(x) cat(n) # n is local!}n <- 5 # Define n in the *global* environmentprint_x(5)
## 5
print_x <- function(x, n = NULL) { cat(x) cat(n) # n is local!}n <- 5 # Define n in the *global* environmentprint_x(5)
## 5
n <- 6print_x(5)
## 5
Use n
as argument:
print_x(5, n)
## 56
02:00
Consider this code:
x <- 7y <- NULLf1 <- function(x) { cat(x^3) cat(y, x)}f2 <- function(x, y = 7) { cat(x^3, y)}f3 <- function(x, y) { cat(x^3) cat(y)}f4 <- function(x) { return(x^3) cat(x^4)}
What will these lines of code produce?
Write your answer down first, then run the code to check.
f1(2)f2(2)f3(2)f4(2)
05:00
Example: Given values a
and b
, find the value c
such that the triangle formed by lines of length a
, b
, and c
is a right triangle (in short, find hypotenuse)
Example: Given values a
and b
, find the value c
such that the triangle formed by lines of length a
, b
, and c
is a right triangle (in short, find hypotenuse)
Example: Given values a
and b
, find the value c
such that the triangle formed by lines of length a
, b
, and c
is a right triangle (in short, find hypotenuse)
hypotenuse <- function(a, b) { return(sqrt(sumOfSquares(a, b)))}
sumOfSquares <- function(a, b) { return(a^2 + b^2)}
10:00
Create a function, isRightTriangle(a, b, c)
that returns TRUE
if the triangle formed by the lines of length a
, b
, and c
is a right triangle and FALSE
otherwise. Use the hypotenuse(a, b)
function in your solution. Hint: you may not know which value (a
, b
, or c
) is the hypotenuse.
hypotenuse <- function(a, b) { return(sqrt(sumOfSquares(a, b)))}
sumOfSquares <- function(a, b) { return(a^2 + b^2)}
V1:
sumofsquares<-function(a,b)return(a^2 + b^2)
V2:
sum_of_squares <- function(a, b) { return(a^2 + b^2)}
V1:
sumofsquares<-function(a,b)return(a^2 + b^2)
V2: <- This one is much better!
sum_of_squares <- function(a, b) { return(a^2 + b^2)}
<-
for assignment, not =
x <- 1
, not x<-1
)hw1.R
" vs. "untitled.R
")Generally, function names should be verbs:
add() # Goodaddition() # Bad
Generally, function names should be verbs:
add() # Goodaddition() # Bad
Avoid using the ".
" symbol:
get_hypotenuse() # Goodget.hypotenuse() # Bad
Generally, function names should be verbs:
add() # Goodaddition() # Bad
Avoid using the ".
" symbol:
get_hypotenuse() # Goodget.hypotenuse() # Bad
Use curly braces, with indented code inside:
sum_of_squares <- function(a, b) { return(a^2 + b^2)}
15:00
onesDigit(x)
: Write a function that takes an integer and returns its ones digit.
Tests:
tensDigit(x)
: Write a function that takes an integer and returns its tens digit.
Tests:
You may want to use onesDigit(x)
as a helper function for tensDigit(x)
The mod operator (%%
) "chops" a number and returns everything to the right
123456 %% 1
## [1] 0
123456 %% 10
## [1] 6
The integer divide operator (%/%
) "chops" a number and returns everything to the left
123456 %/% 1
## [1] 123456
123456 %/% 10
## [1] 12345
15:00
eggCartons(eggs)
: Write a function that reads in a non-negative number of eggs and prints the number of egg cartons required to hold that many eggs. Each egg carton holds one dozen eggs, and you cannot buy fractional egg cartons.
militaryTimeToStandardTime(n)
: Write a function that takes an integer between 0 and 23 (representing the hour in military time), and returns the same hour in standard time.
#classroom
channel in Slack for link06:00
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
o | Tile View: Overview of Slides |
Esc | Back to slideshow |
#classroom
channel in Slack for link06:00
It's nice to see your faces :)
If you're okay with it, please turn on your camera - it creates a more engaging discussion environment and an opportunity for us to get to know each other better. Your privacy is important though, and I understand if you wish to keep it off. No pressure.
functionName <- function(arguments) { # Do stuff here return(something)}
In English:
"
functionName
is afunction
ofarguments
that does..."
functionName <- function(arguments) { # Do stuff here return(something)}
Example:
"
squareRoot
is afunction
ofn
that...returns the square root ofn
"
squareRoot <- function(n) { return(n^0.5)}
Example:
"
squareRoot
is afunction
ofn
that...returns the square root ofn
"
squareRoot <- function(n) { return(n^0.5)}
squareRoot(64)
## [1] 8
return()
and cat()
statementsreturn()
and cat()
statementsisPositive <- function(n) { return(n > 0)}
return()
and cat()
statementsisPositive <- function(n) { return(n > 0)}
isPositive <- function(n) { cat(n > 0)}
return()
and cat()
statementsisPositive <- function(n) { return(n > 0)}
return()
returns back a value
test <- isPositive(7)test
TRUE
isPositive <- function(n) { cat(n > 0)}
cat()
prints a value as a string
test <- isPositive(7)
TRUE
test
Error: object 'test' not found
cat()
is short for "concatenating"cat()
is short for "concatenating"print_x <- function(x) { cat("The value of x is", x)}
cat()
is short for "concatenating"print_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
cat()
is short for "concatenating"print_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
print_x_squared <- function(x) { cat("The value of x is", x, "and the value of x^2 is", x^2)}
cat()
is short for "concatenating"print_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
print_x_squared <- function(x) { cat("The value of x is", x, "and the value of x^2 is", x^2)}
print_x_squared(7)
## The value of x is 7 and the value of x^2 is 49
cat()
adds a space between values by defaultcat()
adds a space between values by defaultprint_x <- function(x) { cat("The value of x is", x)}
cat()
adds a space between values by defaultprint_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
cat()
adds a space between values by defaultprint_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
Modify separator with the sep
argument:
print_x <- function(x) { cat("The value of x is", x, sep = ": ")}
cat()
adds a space between values by defaultprint_x <- function(x) { cat("The value of x is", x)}
print_x(7)
## The value of x is 7
Modify separator with the sep
argument:
print_x <- function(x) { cat("The value of x is", x, sep = ": ")}
print_x(7)
## The value of x is: 7
02:00
Consider these functions:
f1 <- function(x) { return(x^3)}f2 <- function(x) { cat(x^3)}f3 <- function(x) { cat(x^3) return(x^4)}f4 <- function(x) { return(x^3) cat(x^4)}
What will these lines of code produce?
Write your answer down first, then run the code to check.
f1(2)f2(2)f3(2)f4(2)
squareOfX <- function(x) { y <- x^2 # y here is "local" return(y)}
squareOfX <- function(x) { y <- x^2 # y here is "local" return(y)}
squareOfX(3)
## [1] 9
If you try to call y
, you'll get an error:
y
Error: object 'y' not found
print_x <- function(x) { cat(x) cat(n) # n is global!}n <- 5 # Define n in the *global* environmentprint_x(5)
## 55
print_x <- function(x) { cat(x) cat(n) # n is global!}n <- 5 # Define n in the *global* environmentprint_x(5)
## 55
n <- 6print_x(5)
## 56
Function behavior shouldn't change with the same arguments!
print_x <- function(x, n = NULL) { cat(x) cat(n) # n is local!}n <- 5 # Define n in the *global* environmentprint_x(5)
## 5
print_x <- function(x, n = NULL) { cat(x) cat(n) # n is local!}n <- 5 # Define n in the *global* environmentprint_x(5)
## 5
n <- 6print_x(5)
## 5
Use n
as argument:
print_x(5, n)
## 56
02:00
Consider this code:
x <- 7y <- NULLf1 <- function(x) { cat(x^3) cat(y, x)}f2 <- function(x, y = 7) { cat(x^3, y)}f3 <- function(x, y) { cat(x^3) cat(y)}f4 <- function(x) { return(x^3) cat(x^4)}
What will these lines of code produce?
Write your answer down first, then run the code to check.
f1(2)f2(2)f3(2)f4(2)
05:00
Example: Given values a
and b
, find the value c
such that the triangle formed by lines of length a
, b
, and c
is a right triangle (in short, find hypotenuse)
Example: Given values a
and b
, find the value c
such that the triangle formed by lines of length a
, b
, and c
is a right triangle (in short, find hypotenuse)
Example: Given values a
and b
, find the value c
such that the triangle formed by lines of length a
, b
, and c
is a right triangle (in short, find hypotenuse)
hypotenuse <- function(a, b) { return(sqrt(sumOfSquares(a, b)))}
sumOfSquares <- function(a, b) { return(a^2 + b^2)}
10:00
Create a function, isRightTriangle(a, b, c)
that returns TRUE
if the triangle formed by the lines of length a
, b
, and c
is a right triangle and FALSE
otherwise. Use the hypotenuse(a, b)
function in your solution. Hint: you may not know which value (a
, b
, or c
) is the hypotenuse.
hypotenuse <- function(a, b) { return(sqrt(sumOfSquares(a, b)))}
sumOfSquares <- function(a, b) { return(a^2 + b^2)}
V1:
sumofsquares<-function(a,b)return(a^2 + b^2)
V2:
sum_of_squares <- function(a, b) { return(a^2 + b^2)}
V1:
sumofsquares<-function(a,b)return(a^2 + b^2)
V2: <- This one is much better!
sum_of_squares <- function(a, b) { return(a^2 + b^2)}
<-
for assignment, not =
x <- 1
, not x<-1
)hw1.R
" vs. "untitled.R
")Generally, function names should be verbs:
add() # Goodaddition() # Bad
Generally, function names should be verbs:
add() # Goodaddition() # Bad
Avoid using the ".
" symbol:
get_hypotenuse() # Goodget.hypotenuse() # Bad
Generally, function names should be verbs:
add() # Goodaddition() # Bad
Avoid using the ".
" symbol:
get_hypotenuse() # Goodget.hypotenuse() # Bad
Use curly braces, with indented code inside:
sum_of_squares <- function(a, b) { return(a^2 + b^2)}
15:00
onesDigit(x)
: Write a function that takes an integer and returns its ones digit.
Tests:
tensDigit(x)
: Write a function that takes an integer and returns its tens digit.
Tests:
You may want to use onesDigit(x)
as a helper function for tensDigit(x)
The mod operator (%%
) "chops" a number and returns everything to the right
123456 %% 1
## [1] 0
123456 %% 10
## [1] 6
The integer divide operator (%/%
) "chops" a number and returns everything to the left
123456 %/% 1
## [1] 123456
123456 %/% 10
## [1] 12345
15:00
eggCartons(eggs)
: Write a function that reads in a non-negative number of eggs and prints the number of egg cartons required to hold that many eggs. Each egg carton holds one dozen eggs, and you cannot buy fractional egg cartons.
militaryTimeToStandardTime(n)
: Write a function that takes an integer between 0 and 23 (representing the hour in military time), and returns the same hour in standard time.