|
Since R is a highly
functional language, everything in R can be accomplished by using or writing
functions. Rules for writing functions are very simple. You can declare a
function like this: myfunction <- function(){statements}, where myfunction
is the object that will be outcome of writing the function. {} indicate
opening and closing of statements of the function. These are not mandatory,
for instance if you plan to write a simple one-liner function, you need not
put curly brackets. However, we recommend putting curly brackets to write
functions for they are good practices. Semicolons are not necessary unless
you want to separate two pieces of codes written in the same line.
In terms of programming in R, R itself comes with a rich set of pre-built
functions that you can use. For instance, functions like "apply", "sapply",
"tapply" accomplish looping actions quite effectively. Read about these
functions by using commands like ?apply, ?sapply, ?tapply in an R session. |