100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
STAT 404 - MIDTERM 2 Terms in this set (44) Why do we need functions? Data structures tie related values into one object Functions tie related commands into one object In both cases: easier to understand, easier to work with, easier to build int $5.99   Add to cart

Exam (elaborations)

STAT 404 - MIDTERM 2 Terms in this set (44) Why do we need functions? Data structures tie related values into one object Functions tie related commands into one object In both cases: easier to understand, easier to work with, easier to build int

 6 views  0 purchase
  • Course
  • CGFO - Certified Government Finance Officer
  • Institution
  • CGFO - Certified Government Finance Officer

STAT 404 - MIDTERM 2 Terms in this set (44) Why do we need functions? Data structures tie related values into one object Functions tie related commands into one object In both cases: easier to understand, easier to work with, easier to build into larger things Function struc...

[Show more]

Preview 2 out of 9  pages

  • August 3, 2024
  • 9
  • 2024/2025
  • Exam (elaborations)
  • Questions & answers
  • CGFO - Certified Government Finance Officer
  • CGFO - Certified Government Finance Officer
avatar-seller
Denyss
8/3/24, 2:01 PM



STAT 404 - MIDTERM 2
Jeremiah


ama




Terms in this set (44)

Data structures tie related values into one object


Functions tie related commands into one object
Why do we need functions?

In both cases: easier to understand, easier to work with, easier to build into larger
things

The structure of a function has three basic parts:
Function structure
- Name Inputs (or arguments): what should a user provide to the function?
- Arguments
- Body Body: code that is executed
- Return
Output (or return value): what is the side effect of your function

Things you're going to re-run, especially if it will be re-run with changes to arguments


What should be a function? Chunks of code which are small parts of bigger analyses


Chunks of code which are very similar to other chunks1

Test code outside a function first


Put code in a function and test


Best practices when creating a function Replace hard coded values with arguments and test again


Use parenthesis liberally


Only include in the function what can be repeate

With no explicit return() statement, the default is just to return whatever is on the last
What is the default return value?
line

A function can also specify default values for the inputs (if the user doesn't specify an
How and when to use default inputs
input in the function call, then the default value is used)




1/9

, 8/3/24, 2:01 PM
Inputs can be called by name, or without


Calling functions we define works just like Inputs can be called by partial names (if uniquely
calling built-in functions: named arguments,
default. When inputs aren't specified, default values are


Named inputs can go in any order

While named inputs can go in any order, unnamed inputs must go in the proper order
(as they are specified in the function's definition).
How are argument values matched to
arguments when a function is called?
When calling a function with multiple arguments, use input names for safety, unless
you're absolutely certain of the right order for (some) inputs10

When creating a function in R, though you cannot return more than one output, you
How to return more than one output
can return a list. This (by definition) can contain an arbitrary number of arbitrary objects

A side effect of a function is something that happens as a result of the function's body,
but is not returned.Examples:


Printing something out to the console
What is a side effect?

Plotting something on the display


Saving an R data file, or a PDF, etc.

Interfaces mark out a controlled inner environment for our code


Interact with the rest of the system only at the interface
Interfaces control what the function can see
(arguments, environment) and change (its Advice: arguments explicitly give the function all the information
internals, its return value) - Reduces risk of confusion and error
- Exception: true universals like π


Likewise, output should only be through the return value

In R, functions are objects, just like everything else

What do we mean by functions are objects?
This means that they can be passed to functions as arguments and returned by
functions as outputs as well

Beyond regular functions, we can use


Functionals: functions that take another function as
an argument (like the apply family). Most likely to
use
What are the four higher order classifications
of functions?
Function factories: functions that create functions
(like ecdf()). Less common.


Function operators: functions that take functions as
input and output a function




2/9

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller Denyss. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $5.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

77254 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$5.99
  • (0)
  Add to cart