Title: Introduction to R - Functions, Packages
1Introduction to R - Functions, Packages
2Overview
- Functions
- Packages
- Questions
3Functions
- 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
4Functions
- Built in functions (note the lack of parenthesis
this is also why you dont want to name
variables as functions)
gt rep function (x, ...) .Primitive("rep") gt
c function (..., recursive FALSE)
.Primitive("c")
5Functions
- Syntax functionName lt- 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
6Functions
Input
tests if a number if positive pos function(x)
if(x gt 0) out 1 if(x lt 0) out
0 return(out)
Output
7Functions
gt pos function(x) if(x gt 0) out 1 if(x
lt 0) out 0 return(out) gt pos(5) 1 1 gt
pos(-5) 1 0
Run the function first
8Functions
- The value that the function returns can be stored
as a new variable
gt y pos(4) gt y 1 1
9Functions
gt protected x is the input to pos gt x
3 gt pos(6) 1 1 gt x 1 3
10Functions
- Using return() explicitly tells the function what
to return safe - Otherwise, the function will return the last
value that it works with or assigns - This can be an issue if you use a lot of logical
statements
11Functions
- Adding a verbose logical variable gives the
user some feedback
gt pos function(x,verboseTRUE) if(x gt 0)
out 1 if(x lt 0) out 0 if(verbose)
cat("finished running","\n") return(out) gt
y pos(5) finished running gt y 1 1
12Functions
- You can change the input into functions when you
run them - You can name each input, or just put their values
in order
gt y pos(5, verbose FALSE) gt y pos(5, FALSE)
same thing gt y 1 1
13Functions
- Here, we set verbose to be TRUE by default, ie
that that function says what its doing - Using verbose statements throughout your
functions makes it easier to use (especially if
other people are going to use your function)
14Functions
- Note really long for loops also benefit from
using verbose statements - mod, the remainder of the first number
divided by the second - Print a period every 10,000 iterations
for(i in 11e5) if(i 10000 0)
cat(".") do other stuff
15Functions
- Why do functions?
- Provides modularity to your code
- You can solve small problems using functions
- Keeps your actual analysis script cleaner
- You can distribute your code script to other
people
16Functions
- For example, download the lecture notes, and type
source(lecture_notes.R) - The pos function should be added into your
working directory - Another function, mypar was added, but requires
a package!
17Functions
- Make a function that takes two numbers as inputs,
and returns their sum - Make a function that takes three inputs as inputs
and returns their product
18Functions
summ lt- function(a,b) out ab return(out)
prodd lt- function(a,b,c) out
abc return(out)
19Overview
- Functions
- Packages
- Questions
20Packages
- R can access and install a huge library of
packages - The full list is here http//cran.r-project.org/w
eb/packages/ - You only need to install a package once, then you
can use it whenever you want
21Packages
- ?install.packages
- Lets try downloading RColorBrewer
- install.packages(RColorBrewer)
- A list of repository directories should pop-up ?
select USA (basically any of them will work) - I think the Maryland one is Hopkins
22Packages
- Some installation popups occurs and the package
should be installed on your computer - Then, library(package_name)
- library(RColorBrewer) quotes are optional
23Packages
- ?RColorBrewer Creates nice looking color
palettes especially for thematic maps - Top left corner RColorBrewer RColorBrewer
- Top left in mean mean base
- The thing in is the package
24Packages
- Now, resource the lecture 7 notes so that the
mypar() function gets read in correctly - This function will alter the plot parameters to
make the color palette more divergent, changes
the margin sizes, and can allow you to plot
multi-panel plots
25Packages
- mypar() default is 1 x 1 plot space
- mypar(2,2) 2 rows, 2 columns
gt data(cars) gt mypar() gt plot(cars, type "b",
col 5)
26Packages
- I usually google R something Im 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
27Packages
- Try installing those packages now
- Remember, to use a package, you must invoke the
library(package) command
28Packages
- library(survival)
- ?survival
- ??survival fuzzy search
- ?Surv
29Overview
- Functions
- Packages
- Questions
30Questions?
- Any burning R questions? Open floor