prev next front |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |32 |33 |review
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.