Professional Documents
Culture Documents
Functions, Packages
Andrew Jaffe
10/18/10
Overview
Functions
Packages
Questions
Functions
Everything you use in R is a built-in
function
You can download new functions
(packages – next section)
You can add your own to a script
Functions
Built in functions (note the lack of
parenthesis – this is also why you don’t
want to name variables as functions):
> rep
function (x, ...) .Primitive("rep")
>c
function (..., recursive = FALSE)
.Primitive("c")
Functions
Syntax: functionName <- function(inputs) {
body of the function which returns
something}
You must run/submit the function before
you can use it
Note that variable naming within a function
is protected, and not altered by whatever
your script is doing
Functions
Input
> y = pos(4)
> y
[1] 1
Functions
> data(cars)
> mypar()
> plot(cars, type = "b", col = 5)
Packages
I usually google “R + [something I’m trying
to do]” and sometimes links can direct you
to existing packages
The full list is on the CRAN website
Some Epi ones are “survival” – survival
analysis, “lme4” – longitudinal data
analysis, “Epi” – some epi functions
Packages
Try installing those packages now
Remember, to use a package, you must
invoke the library(package) command
Packages
library(survival)
?survival
??survival – “fuzzy search”
?Surv
Overview
Functions
Packages
Questions
Questions?
Any burning R questions? Open floor…