It does work and it's R's fault!

So how can you shift your thinking to get moving again when you're stuck and frustrated?

Play, play and play some more!

  • Footnote: about the most descructive thing you can do is erase all of your files. Please don't do that.

Try to invalidate your assumptions

Two quick-and-dirty tools that can be useful in testing your assumptions are print and class.

f <- function(a, b) {
  
  c <- a - 1
  
  b / c
}

f(2, 1)
## [1] 1
# This returns something crazy!
f(1, 2)
## [1] Inf
f <- function(a, b) {
  
  c <- a - 1
  
  print(c)
  
  b / c
}

f(2, 1)
## [1] 1
## [1] 1
# This returns something crazy!
f(1, 2)
## [1] 0
## [1] Inf

So we've made our denominator; hence our wierd division result.

f <- function(a, b) {
  
  c <- a - 1
  d <- b / c
  
}

f(2, 1)

Why no number R??? Well, how about we capture our function's return in a variable:

e <- f(2, 1)
class(e)
## [1] "numeric"

What is it? Can we make sense of that?

print(e)
## [1] 1

Here's the function you probably meant to implement:

f <- function(a, b) {
  
  c <- a - 1
  b / c
  
}