wherefunction name_function(arguments){expressions}

std.dev_function(x) { std.x_sqrt(var(x)) std.x }The last line in the function gives the name of the value to be returned by the function.

> std.dev(c(1,2,3,4,5)) > [1] 1.581139

if(If the logical expressioncondition)expr1or if(condition)expr1elseexpr2

> x_2 >if (x > 0) log(x)[1] 0.6931472 >if (x > 100) (x - 100)NULL >if (x < 0) x else log(x)[1] 0.6931472

&&It is not always necessary to evaluate both sides of a logical expression using & or |. The sequential operators && and || take advantage of this to avoid errors which could crash the function. If the left operand is F, && returnssequential and||sequential or

> y_"Jo" >If the first logical expression is FALSE, evaluating the second expression would cause an error.if (is.numeric(y) && (y > 0)) log(y)> NULL

if (y > 0) log(y) elsewarning() prints a warning message but does not cause the function to be exited:stop("y is not greater than 0")

> x_c(1,2,3,4,5,NA) > if (sum(is.na(x)) > 0) {x_x[!is.na(x)] +warning("Missing values have been deleted")}Warning messages: Missing values have been deleted > x [1] 1 2 3 4 5

y_switch(transformation, logarithm = y_log(x), "square root" = y_sqrt(x), stop("Invalid transformation"))Here,

for(where the curly brackets are optional when only one expression is specified.indexinvalues) {expressions}

> x_0 >for() loops may also be used to construct data objects by usingfor(i in 1:10) x_x+i> x [1] 55

> rm(x) > for(i in 1:10) x[i]_i Error: Object "x" not found >To create a matrix using two for() loops, a null matrix of the correct dimensions must be created outside the for() loops.x_NULL>for(i in 1:10) x[i]_i> x [1] 1 2 3 4 5 6 7 8 9 10

>x_matrix(0, nrow=3, ncol=4)> for(i in 1:3) { + for(j in 1:4) {x[i,j]_i+j}} > x [,1] [,2] [,3] [,4] [1,] 2 3 4 5 [2,] 3 4 5 6 [3,] 4 5 6 7

while(Notice that in order for the last value of x to be less than 1000000, the condition in the while() expression must becondition) {expressions} > while(x*2 < 1000000) { x_x*2 + i_i+1} > x [1] 524288 > i [1] 18

> x_2 > i_0 > repeat{ i_i+1 x_x*2 if(x*2 > 1000000) break} > x [1] 524288 > i [1] 18repeat() will keep repeating the expressions forever unless some condition is set within the iteration to break out of the loop. Here, a break expression terminates the loop. When Splus evaluates a break, it exits the innermost enclosing for(), while(), or repeat() loop.

mean_function(x, trim = 0, na.rm = F)Specifying default values for some of the arguments in a function allows the user to omit those arguments when making a call to the function. As seen in section 4, typing

> **mean(x)**

returns a regular mean, and specifying the *trim=* argument, for example

> **mean(x, trim=0.2)**

will return a trimmed mean. The same arguments can be applied to the *na.rm=*
argument.

Objects which are created or modified inside a function are temporary, have no effect outside the function, and disappear when the evaluation of the function is complete. There are several ways of returning the value of a function, one of which is to name the value to be returned on the last line of the function. This value can be stored permanently by assigning a name to the function evaluation. For example:

> x.mean_mean(x)creates a data object,

example_function(x) { x.mean <- mean(x) x.var <- var(x) x.sum <- sum(x)return(x.mean, x.var, x.sum)} > x_c(1,2,3,4,5) > example(x) $x.mean: [1] 3 $x.var: [1] 2.5 $x.sum: [1] 15

> y_NULL > for(i in 1:3){ + y_y+i +cat("i is", i, "\n")}i is 1 i is 2 i is 3

example_function(){ cat("Type in the name of the distribution from which to generate","\n") cat("10 random variables","\n") cat("normal or uniform:")distrib_readline()switch(distrib, normal = rnorm(10), uniform = runif(10), stop("Invalid choice"))}

example_function(){ cat("Enter 10 numbers","\n")The scan() function can be used to read in numeric data into a matrix. Suppose the filex_scan(n=10)total_sum(x) cat("The total is",total,"\n")}

>To read in character data, the argumentrain_matrix(scan("rain"),ncol=5, byrow=T)

>The source() function may be used to read in any Splus expression. Rather than typing the expressions directly into Splus, they can be stored in a file and evaluated using the source() function. Suppose the filesource("ex1")

friends_c("Jack","Jill") phones_c(5554321,5551234) cat(friends[1],phones[1],"\n") cat(friends[2],phones[2],"\n")The expressions are then evaluated using the source() function:

>Unlike data objects created within a function, those created from a file outside Splus and evaluated using source() remain in the .Data directory after the expressions have been evaluated:source("ex2")Jack 5554321 Jill 5551234

> friends [1] "Jack" "Jill"