This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

Functions

Computing for Data Analysis

1 / 36

so that you can deﬁne a function inside of another function The return value of a function is the last expression in the function body to be evaluated. f <. In particular. 2 / 36 . Functions can be passed as arguments to other functions Functions can be nested.Functions Functions are created using the function() directive and are stored as R objects just like anything else. Importantly. which means that they can be treated much like any other R object.function(<arguments>) { ## Do something interesting } Functions in R are “ﬁrst class objects”. they are R objects of class “function”.

The formal arguments are the arguments included in the function deﬁnition The formals function returns a list of all the formal arguments of a function Not every function call in R makes use of all the formal arguments Function arguments can be missing or might have default values 3 / 36 .Function Arguments Functions have named arguments which potentially have default values.

rm = FALSE.rm = FALSE) sd(na.rm = FALSE. na. 4 / 36 . x = mydata) sd(na. since it can lead to some confusion. So the following calls to sd are all equivalent > > > > > > mydata <.rnorm(100) sd(mydata) sd(x = mydata) sd(x = mydata. mydata) Even though it’s legal. I don’t recommend messing around with the order of the arguments too much.Argument Matching R functions arguments can be matched positionally or by name.

na. x = FALSE. subset. y ~ x. weights. it is “taken out” of the argument list and the remaining unnamed arguments are matched in the order that they are listed in the function deﬁnition. model = FALSE. . 1:100) lm(y ~ x. offset. > args(lm) function (formula... When an argument is matched by name.Argument Matching You can mix positional matching with matching by name. y = FALSE. singular. contrasts = NULL. qr = TRUE. mydata. model = FALSE) 5 / 36 . lm(data = mydata. method = "qr".) The following two calls are equivalent. data.ok = TRUE. model = TRUE. 1:100.action.

Argument Matching Most of the time. 6 / 36 . named arguments are useful on the command line when you have a long argument list and you want to use the defaults for everything except for an argument near the end of the list Named arguments also help if you can remember the name of the argument and not its position on the argument list (plotting is a good example).

which is useful for interactive work. The order of operations when given an argument is 1 2 3 Check for exact match for a named argument Check for a partial match Check for a positional match 7 / 36 .Argument Matching Function arguments can also be partially matched.

function(a.Deﬁning a Function f <. you can also set an argument value to NULL. c = 2. 8 / 36 . b = 1. d = NULL) { } In addition to not specifying a default value.

f <.Lazy Evaluation Arguments to functions are evaluated lazily. 9 / 36 . b) { a^2 } f(2) This function never actually uses the argument b. so calling f(2) will not produce an error because the 2 gets positionally matched to a.function(a. so they are evaluated only as needed.

with no default > Notice that “45” got printed ﬁrst before the error was triggered.function(a.Lazy Evaluation Another example f <. This is because b did not have to be evaluated until after print(a). 10 / 36 . b) { print(a) print(b) } > f(45) [1] 45 Error in print(b) : argument "b" is missing. Once the function tried to evaluate print(b) it had to throw an error.

type = "l". y.” Argument The ..) { plot(x. y.. type = type.... . .. > mean function (x. argument indicate a variable number of arguments that are usually passed on to other functions.. is often used when extending another function and you don’t want to copy the entire argument list of the original function myplot <... so that extra arguments can be passed to methods (more on this later).) UseMethod("mean") 11 / 36 ...) } Generic functions use . ..function(x.The “. ...

..The “. sep = " ".. > args(paste) function (. append = FALSE) 12 / 36 . file = "".. argument is also necessary when the number of arguments passed to the function cannot be known in advance.” Argument The .... collapse = NULL) > args(cat) function (. labels = NULL. fill = FALSE.. sep = " "...

. "b". > args(paste) function (.. on the argument list must be named explicitly and cannot be partially matched... collapse = NULL) > paste("a".. sep = " ". is that any arguments that appear after .. "b".” Argument One catch with ..Arguments Coming After the “.. sep = ":") [1] "a:b" > paste("a".. se = ":") [1] "a b :" 13 / 36 .

- Do New Prices Reflect
- Which is the world's most expensive city Cost of living survey 2012 News theguardian.com
- Data Analysis Final
- 1
- 1
- S program
- Lecture 7
- Rplot
- Data Analysis Plot
- Data Analysis
- Lapply
- Regular Expressions in R
- debugging in R
- A Brief History of S
- Untitled
- R Functions
- hospital
- S-history
- Understanding Health Statistics
- Insisting on Beautiful Maps (FlowingData, 25 Oct. 2012) for R Presentation
- An Example of Simpson s Paradox
- A Review of Software Packages for Analizing Correlated Survival Data
- Use of R as a Toolbox for Mathematical and Statistics Exploration
- Ignoring a Covariate an Example of Simpson s Paradox

Basics of R functions

Basics of R functions

- hw2_sol
- J-87-11 Paper - III
- The Relational Data Model and Relational Database Constraints
- Wishful Search
- Midterm Exam
- UGC Computer Science Paper 3 June 2011
- Data Mining Presentation
- Chapter-3, EMC DSA
- DAA.pdf
- Readme.cplex
- A Handbook of Statistical Analyses Using Stata
- Worst Case Efficient Data Structures
- Caret
- daa
- Python for Data Analysis Table of Contents 2012-06-25
- La cuarta lectura
- T-Digest Algorithm
- icde12mqo
- Data Structures
- Witness 2006
- Branch and Bound Algorithms
- smo (1)
- sigmod295-herodotou
- Algorithms Tutorials
- uuuuuuuu
- An Effective Implementation of Kopt Moves for the LK TSP
- What Are Stacks and Queues
- Video Suggestion and Discovery for YouTube
- Fast Computation of the Median by Successive Binning

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd