Writing Functions in R Introduction to R, Part II

Slides:



Advertisements
Similar presentations
Programming Methodology (1). Iteration Learning objectives explain the term iteration; repeat a section of code with a for loop; repeat a section of.
Advertisements

Reviewing your Program CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4 © Mitchell Wand, This work is licensed under a Creative Commons.
IT151: Introduction to Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Welcome! The Topic For Today Is…Review for Test. Your Topic Basics of Programming Control Statements Programming Concepts InteractionAlice Concepts 200.
Introduction to Python
The Software Development Life Cycle. Software Development SDLC The Software Development Life-Cycle Sometimes called the program development lifecycle.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Introduction to Testing CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
INTRODUCTION TO COMPUTER PROGRAMMING(IT-303) Basics.
Software Engineering Algorithms, Compilers, & Lifecycle.
Students can reflect on how life online can idealised and may not reflect reality
Introduction to Computing Science and Programming I
Chapter 10 Programming Fundamentals with JavaScript
Computer Programming.
Topic: Programming Languages and their Evolution + Intro to Scratch
Lesson 5-2 AP Computer Science Principles
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Reviewing your Program
Controlling execution - iteration
A Lecture for the c++ Course
Beginning C++ Programming
The Pseudocode Programming Process
Functions & Relations.
Topic: Functions – Part 2
Systems Analysis and Design
From Templates to Folds
Looping and Random Numbers
Communication Technology
Vocabulary Algorithm - A precise sequence of instructions for processes that can be executed by a computer.
Algorithm and Ambiguity
An Introduction to Visual Basic .NET and Program Design
Utilization Utilization.
BEGINNER PROGRAMMING LESSON
Parallel Beam Synchronization
Standards for Mathematical Practice
Lecture 1: Introduction
Additional Resources Introduction to R, Part II
Introduction to pseudocode
Parallel Beam Synchronization
How to Run a Java Program
Exploring Data in R Introduction to R, Part II
CSCE 489- Problem Solving Programming Strategies Spring 2018
Data: Food for functions in the Realm of R
BEGINNER PROGRAMMING LESSON
Chapter 6: Repetition Statements
Installing Packages Introduction to R, Part II
Vlookup.
Algorithm and Ambiguity
What is a Function Teacher Notes
Program Design Language (PDL)
BEGINNER PROGRAMMING LESSON
Software Development Process
Getting Data In and Out of R Introduction to R, Part II
PYTHON: BUILDING BLOCKS Sequencing & Selection
CMSC201 Computer Science I for Majors Final Exam Information
An Introduction to Debugging
STRING CHALLENGE.
Functions: Creatures that live in the Realm of R
Computational Thinking
Chapter 1: Programming Basics, Python History and Program Components
Introduction to Python
Chapter 3 Debugging Section 3.4
U3L2 The Need For Algorithms
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review
Programming Techniques
Presentation transcript:

Writing Functions in R Introduction to R, Part II Anna Blackstock Statistician, Biostatistics and Information Management Office (BIMO) NCEZID/DFWED

What are functions? “Functions in R are objects that carry out operations on arguments … and return one or more values.” Syntax: From “The R Book” by Michael J. Crawley, Wiley Publishing, 2007.

Anatomy of Creatures in the Land of R: Functions ( ) arguments { } From Beau Bruce’s PowerPoint presentation “functions.” instructions output

input data ( ) function {} output data

Example of an R Function Take a look at this function: What are the arguments? What does this function do? What is a better name for this function?

When should you write functions? Functions can save you time and prevent copy/paste errors if you are repeating a task. Rule of thumb: any time you will do something > 3 times, think about writing a function!

How to Get Started Would it be easier to: Write a general function, and then try to apply it to a particular problem, or Write code to solve a particular problem, and then generalize it so that it can be used many times?

How to Get Started Would it be easier to: Write a general function, and then try to apply it to a particular problem, or Write code to solve a particular problem, and then generalize it so that it can be used many times?

How to Get Started Would it be easier to: Write a general function, and then try to apply it to a particular problem, or Write code to solve a particular problem, and then generalize it so that it can be used many times? Advice: the best way to start writing a function is to NOT start writing a function. See http://r4ds.had.co.nz/functions.html and “Expressing Yourself with R” at http://bit.ly/hadley-express-yourself (licensed under Creative Commons Attribution-Noncommercial 3.0 United States License).

General Rules for Writing Functions Solve the problem before trying to write a function. Give your function an appropriate name. Make sure your function is correct and know when it fails. Reserve comments for the “why.”

Where to from here? If you’d like to learn more, this interactive course (taught by siblings Hadley and Charlotte Wickham) might be a good place to start: https://www.datacamp.com/courses/writing-functions-in-r

References R for Data Science book by Grolemund and Wickham: http://r4ds.had.co.nz/functions.html Hadley Wickham’s “Expressing Yourself with R” presentation: http://bit.ly/hadley-express-yourself Licensed under Creative Commons Attribution-Noncommercial 3.0 United States License.