Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to R - Functions, Packages Andrew Jaffe 10/18/10."— Presentation transcript:

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 don’t want to name variables as functions): > rep function (x,...).Primitive("rep") > c function (..., recursive = FALSE).Primitive("c")

5 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

6 Functions # tests if a number if positive pos = function(x) { if(x >= 0) out = 1 if(x < 0) out = 0 return(out) } Input Output

7 Functions > pos = function(x) { + if(x >= 0) out = 1 + if(x < 0) out = 0 + return(out) + } > pos(5) [1] 1 > pos(-5) [1] 0 Run the function first

8 Functions The value that the function returns can be stored as a new variable > y = pos(4) > y [1] 1

9 Functions > # protected – x is the # input to pos > x = 3 > pos(6) [1] 1 > 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 > pos = function(x,verbose=TRUE) { + if(x >= 0) out = 1 + if(x < 0) out = 0 + + if(verbose) cat("finished running","\n") + return(out) + } > y = pos(5) finished running > 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 > y = pos(5, verbose = FALSE) > y = pos(5, FALSE) # same thing > 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 1:1e5) { 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 <- function(a,b) { out = a+b return(out) } prodd <- function(a,b,c) { out = a*b*c 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/web/packages/http://cran.r- project.org/web/packages/ You only need to install a package once, then you can use it whenever you want

21 Packages ?install.packages Let’s 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 > data(cars) > mypar() > plot(cars, type = "b", col = 5)

26 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

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…


Download ppt "Introduction to R - Functions, Packages Andrew Jaffe 10/18/10."

Similar presentations


Ads by Google