ABSTRACT

If a user wants a function that is not available in an existing package in the exact format that is need, then the user has to write it on his/her own. This chapter deals with writing simple functions, and presents an introduction to date objects, useful base R functions cut() and seq(), and a shortcut to create character vectors, Hmisc::Cs(). It also presents if-then conditions in R and a peek at automated testing. function() says that the myfunction variable is going to be a function and not simply storing a vector or data frame. That part has to say function, and function() has to have parentheses after it. The chapter discusses the basics of custom functions with dates: Sys.Date() gives the user today’s date; months() is a base R function that allows date calculations by months; and cut() is a base R function which separates a range of values into intervals.