If $x = (x_1,...,x_{100}),$ and you want to compute $y =
(\sin x_1,...,\sin x_{100}),$ then you write a for-loop in
C. However, in R just use a single line:
y = sin(x)
You should use such "implicit" loops whenever you can, as they
are more efficient than the "explicit" for-loops.
This is the function $f(x) = \sin x-e^x.$ You may think of
this function is $f:{\mathbb R}\rightarrow{\mathbb R}$ or rather
as $f:{\mathbb R}^n\rightarrow{\mathbb R}^n$ for every $n\in{\mathbb N}$ because R
will automatically apply the function of each entry in an
array. For example,
f = function(x,y,z) {
x-y+z
}
g = function() {
printf('hello!')
}
Here is a function from ${\mathbb R}$ to ${\mathbb R}^2:$
f = function(theta) {
c(cos(theta), sin(theta))
}
Some functions in R have many arguments (even 100). It becomes
very difficult to remember the position of all of them. R has two
ways to deal with this problem: default values and labels.
If I write
f = function(x, y=30, z) {
cat('x=',x,'y=',y,'z=',z)
}