Objects and Functions
What we normally do is create objects and then perform functions on those objects
(functions are also considered objects, more on this later).
Assign an object a name "x" using either
x <- object (object -> x)
x = object
Call a function by
function name(list of arguments separated by commas)
I Each function has a set of formal arguments some with default values;
these can be found in the function’s documentation.
I A function call can include any subset of the complete argument list.
I To specify a particular argument use the argument’s name.
I Arguments do not have to be named if they are entered in the same
order as the function’s formal argument list. However, to make your
code easier to understand it is usually a good idea to name your
arguments.
I When specifying values for an argument use an =.
R is CASE SENSITIVE.
Example - Assigning Objects and Function Calls
To find the mean of a set of numbers first assign the vector of numbers a
clever name x and then call the function mean().
> x = c(0,5,7,9,1,2,8)
> x
[1] 0 5 7 9 1 2 8
> mean(x)
[1] 4.571429
> X
Error: object ’X’ not found
To sort a vector y so that the numbers are descending, R will sort ascending
need to change the formal argument decreasing to TRUE (the default value
for decreasing is FALSE).
> y <- c(4,2,0,9,5,3,10)
> y
[1] 4 2 0 9 5 3 10
> sort(y)
[1] 0 2 3 4 5 9 10
> sort(y, decreasing=TRUE)
[1] 10 9 5 4 3 2 0