Introduction to R - Functions, Packages - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to R - Functions, Packages

Description:

Functions Make a function that takes ... and returns their product Functions Overview Functions Packages Questions Packages R can access and install a huge library of ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 31
Provided by: andrewj
Category:

less

Transcript and Presenter's Notes

Title: Introduction to R - Functions, Packages


1
Introduction to R - Functions, Packages
  • Andrew Jaffe
  • 10/18/10

2
Overview
  • Functions
  • Packages
  • Questions

3
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

4
Functions
  • 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")
5
Functions
  • 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

6
Functions
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
7
Functions
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
8
Functions
  • The value that the function returns can be stored
    as a new variable

gt y pos(4) gt y 1 1
9
Functions
gt protected x is the input to pos gt x
3 gt pos(6) 1 1 gt x 1 3
10
Functions
  • 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

11
Functions
  • 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
12
Functions
  • 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
13
Functions
  • 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)

14
Functions
  • 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
15
Functions
  • 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

16
Functions
  • 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!

17
Functions
  • 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

18
Functions
summ lt- function(a,b) out ab return(out)
prodd lt- function(a,b,c) out
abc return(out)
19
Overview
  • Functions
  • Packages
  • Questions

20
Packages
  • 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

21
Packages
  • ?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

22
Packages
  • Some installation popups occurs and the package
    should be installed on your computer
  • Then, library(package_name)
  • library(RColorBrewer) quotes are optional

23
Packages
  • ?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

24
Packages
  • 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

25
Packages
  • 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)
26
Packages
  • 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

27
Packages
  • Try installing those packages now
  • Remember, to use a package, you must invoke the
    library(package) command

28
Packages
  • library(survival)
  • ?survival
  • ??survival fuzzy search
  • ?Surv

29
Overview
  • Functions
  • Packages
  • Questions

30
Questions?
  • Any burning R questions? Open floor
Write a Comment
User Comments (0)
About PowerShow.com