Statistics 16

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Even this works:

> substr(stop=4, start=2,“abcdefg”)


[1] “bcd”
So when you use a function, you can place its arguments out of order, if you name
them. R calls this keyword matching, which comes in handy when you use an R
function that has many arguments. If you can’t remember their order, just use
their names and the function works.
If you ever need help for a particular function — substr() , for example — type
?substr and watch helpful information appear on the Help tab.
User-Defined Functions
Strictly speaking, this is not a book on R programming. For completeness, though,
I thought I’d at least let you know that you can create your own functions in R,
and
show you the fundamentals of creating one.
The form of an R function is
myfunction <- function(argument1, argument2, ... ){
statements
return(object)
}
Here’s a simple function for computing the sum of the squares of three
numbers:
sumofsquares <- function(x,y,z){
sumsq <- sum(c(x^2,y^2,z^2))
return(sumsq)
}
Type that snippet into the Scripts pane and highlight it. Then press Ctrl+R. The
following snippet appears in the Console pane:
> sumofsquares <- function(x,y,z ){
+ sumsq <- sum(c(x^2,y^2,z^2))
+ return(sumsq)
+ }

You might also like