Well-Behaved Functions – Exercises

April 26, 2018
By

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

It is said that, in R, everything that happens is a function call. So, if we want to improve our ability to make things happen the way we want them to, maybe it’s worth getting very comfortable with how functions work in R.
In this exercise set, we’ll try to gain better fluency and deepen our understanding of the R logic by (mostly) writing some functions.
Solutions are available here.

 

Exercise 1
Write a function that calculates number a to the power of b, but let b have a default value of 2.

 

Exercise 2
Re-write the function from exercise 1 so that a has a default value of b+1 already from the formals (from the argument definition.)

 

Exercise 3
Write a function div() that checks if the first number divides the second.

 

Exercise 4
Write an infix function %div% that checks if the left-hand side divides the right-hand side.
# Example
3 %div% 42
[1] TRUE
3 %div% 13
[1] FALSE

 

Exercise 5
Write a function that changes the value of pi in the R global environment to whatever you specify as the argument. Note: it is not recommended to re-define the value of “pi” in a real-life R program.

# Example
pi
[1] 3.141593
chpi(4)
pi
[1] 4
chpi("I like cats")
pi
[1] "I like cats"

 

Exercise 6
Write an infix (binary) function that checks if the left hand side (lhs) is in the range between the maximum and minimum of the rhs.

set.seed(1)
rnorm(6) %betw% c(-1, 1)
[1] TRUE TRUE TRUE FALSE TRUE TRUE

 

Exercise 7
Write another infix operator that pastes two strings and uses it in a function that takes ellipsis. Now, call the function and feed it two strings and concatenate them to produce some text.

 

Exercise 8
What did you think about the example in exercise 3? What does the following code (from the book Advanced R) return?

f2 <- function(x = z) {
z <- 100
x
}
f2()

 

Exercise 9
Write a function that creates a function analogous to the one from exercise 1 by feeding 2 as an argument. Note: this is not a typo.

# Example of a function called power()
powerof2 <- power(2)
powerof2(3)
[1] 9

 

Exercise 10
Make a new names<- (replacement) function called names2<- that also allows you to update the names of data structures, except it forces the names it is given to all-lowercase.

# Example
x <- 3:4
names2(x) <- c("IMPORTANT", "For The CAT do NOT EAT")
x
important for the cat do not eat
3 4

(Photo by Pascal)

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)