Statistical Programming with R

Size: px
Start display at page:

Download "Statistical Programming with R"

Transcription

1 Statistical Programming with R Lecture 5: Simple Programming Bisher M. Iqelan biqelan@iugaza.edu.ps Department of Mathematics, Faculty of Science, The Islamic University of Gaza , Semester 1

2 Functions All computations in R are carried out by calling functions. A call to an R function takes zero or more arguments and returns a single value. Dening functions provides users a way of adding new functionality to R. Functions dened by users have the same status as the functions built into R. Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

3 Dening a function A new function is dened / created by a construct of the form <- function( arglist ) expr where fun.name fun.name is a variable where we store the function. This variable will be used to call the function later arglist is a list of formal arguments. This list can be empty (in which case the function takes no arguments) can have just some names (in which case these names become variables inside the function, whose values have to be supplied when the function is called) can have some arguments in name = value form, in which case the names are variables available inside the function, and the values are their default values expr is an expression (typically an expression block), referred to as the body of a function (which can make use of the variables dened in the argument list). Inside functions, there can be a special return(val) call which exits the function and returns the value val Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

4 Write an R function To understand the concept of writing a new function in R, consider the following very simple example. cube <- function(number) { result <- number * number * number # or number**3 return(result) cube is the name of the function. function is the R function that creates functions. number is the parameter (name of the value) passed to the cube function. { delimits the beginning of the function delimits the end of the function return is the R function that species what the function returns, in this example, the value of result Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

5 Writing Function (empty arguments) > fn <- function(){ # A simple function without argument print("hello") > fn() [1] "hello" Another example: By default the value of the last line is returned. Here, we have a simple function with two objects. The last one is returned. > test <- function() { x <-1 z <- 2 > res <- test() > res [1] 2 Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

6 Writing Function (Empty Arguments Continue..) Consider the following very simple example. Here, the arguments are empty > myvalue <- 1 > myfun1 <- function() { myvalue <- 5 print(myvalue) > myfun1() [1] 5 > myvalue [1] 1 > myfun2 <- function() { print(myvalue) > myfun2() [1] 1 Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

7 Writing Function Continue... Let us dene a function which gives the standard deviation of a vector x. We can create this function as follows. > standard.deviation<- function(x){ sqrt(var(x)) > x <- rnorm(100, mean=0, sd=2) > var(x) [1] > standered.deviation(x) [1] > sd(x) [1] Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

8 Function with several arguments Functions can take several arguments. Suppose we want to calculate x 2 + y 2 for inputs of x and y: > f <- function(x, y) { value <- x^2 + y^2 return(value) > f(x = 2, y = 3) [1] 13 It is a good practice to name the arguments (as we did above) when calling a function, but it is not necessary: > f(5, 0) [1] 25 Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

9 Inx functions Most functions in R are prex operators: the name of the function comes before the arguments. You can also create inx functions where the function name comes in between its arguments, like + or -. All user-created inx functions must start and end with %. For example, one could create a new operator that pastes together strings: > `%+%` <- function(x, y) paste(x, y) > "new" %+% " string" [1] "new string" > `%+%`("new", " string") [1] "new string" Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

10 Example 1 Hence, to obtain the standard.deviation notice that we used two existing functions, the sqrt() and var(). This is a fundamental idea when there is need for dening a new function in R. This example shows how we can use the for and the if function to get the sign of a real number. > new.sign <- function(x) { for (i in 1:length(x)) { x > new.sign(-10:5) if(x[i] > 0) x[i] <- 1 else if(x[i] < 0) x[i] <- -1 [1] Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

11 Example Continue... However a better way is the following which avoids iterations. > sgnfunction <- function(x) { ifelse(x > 0, 1, ifelse(x<0, -1, 0)) > sgnfunction(-10:5) [1] Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

12 Example 2 Consider the following function using if statement: > iffun=function(x){ if (x==2){ print ("x=2") else { print ("x!=2") > iffun(1) [1] "x!=2" > iffun(2) [1] [1] "x=2" Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

13 Example 2 Cont... > iffun2=function(x){ ifelse(x==2, "x=2", "x!=2") > iffun2(1) [1] "x!=2" > iffun2(2) [1] [1] "x=2" Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

14 args() and body() functions In R, args() and body() give the two components of a function: > args(new.sign) function (x) NULL > body(new.sign) { for (i in 1:length(x)) { if (x[i] > 0) x[i] <- 1 else if (x[i] < 0) x[i] <- -1 x Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

15 A function returning a vector Suppose we want to create a function which summarizes a numerical variable in a few statistical terms (mean, variance etc) and returns a vector with these terms. This can be done with > mysummary <- function(x) { value <- c(mean(x), median(x), var(x), min(x), max(x)) return(value) > mysummary(-10:20) [1] Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

16 A function returning a vector Continue... The output of this function is not directly readable (unless we investigate how mysummary was dened). A much more informative output can be obtained by naming the components of the output vector: > mysummary <- function(x) { value <- c(mean = mean(x), med = median(x), var = var(x),min = min(x), max = max(x)) return(value) > mysummary(-10:20) mean med var min max Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

17 A function returning a list A function can also return a list. For example: > mysummary <- function(x) { value <- list(mean = mean(x), med = median(x), var = var(x), min = min(x), max = max(x)) return(value) > (v <- mysummary(cars$speed)) $mean [1] 15.4 $med [1] 15 $var [1] $min [1] 4 $max [1] Bisher M. 25Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

18 Example: Multiple of a number Example, a vector x containing the rst 100 multiples of the number k. > multiple=function(k){ x=0 for(i in 1:100) { x[i]=k*i x # This last entry is the output from the function. # The function only outputs the last line. To run the programs, type multiple(6). This gives the rst 100 multiples of 6. Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

19 Example: Tossing a coin Write a function to simulate a coin toss which allows the user to set p, the probability that the coin shows a head. coin=function(p) { u=runif(1) # Shorthand for U(0,1). if(u<=p) coin="head" if(u>p) coin="tail" coin # This is the output from the program. To run the programs, assuming a fair coin, type coin(0.5). Bisher M. Iqelan (IUG) Lecture 5: Simple Programming 1 st Semester / 18

20 End of lecture 5. Thank you.!!!

Statistical Programming with R

Statistical Programming with R Statistical Programming with R Lecture 6: Programming Examples Bisher M. Iqelan biqelan@iugaza.edu.ps Department of Mathematics, Faculty of Science, The Islamic University of Gaza 2017-2018, Semester 1

More information

Ecffient calculations

Ecffient calculations Ecffient calculations Vectorized computations The efficiency of calculations depends on how you perform them. Vectorized calculations, for example, avoid going trough individual vector or matrix elements

More information

Statistics 251: Statistical Methods

Statistics 251: Statistical Methods Statistics 251: Statistical Methods Summaries and Graphs in R Module R1 2018 file:///u:/documents/classes/lectures/251301/renae/markdown/master%20versions/summary_graphs.html#1 1/14 Summary Statistics

More information

BIO5312: R Session 1 An Introduction to R and Descriptive Statistics

BIO5312: R Session 1 An Introduction to R and Descriptive Statistics BIO5312: R Session 1 An Introduction to R and Descriptive Statistics Yujin Chung August 30th, 2016 Fall, 2016 Yujin Chung R Session 1 Fall, 2016 1/24 Introduction to R R software R is both open source

More information

a suite of operators for calculations on arrays, in particular

a suite of operators for calculations on arrays, in particular The R Environment (Adapted from the Venables and Smith R Manual on www.r-project.org and from Andreas Buja s web site for Applied Statistics at http://www-stat.wharton.upenn.edu/ buja/stat-541/notes-stat-541.r)

More information

Introduction to R. Stat Statistical Computing - Summer Dr. Junvie Pailden. July 5, Southern Illinois University Edwardsville

Introduction to R. Stat Statistical Computing - Summer Dr. Junvie Pailden. July 5, Southern Illinois University Edwardsville Introduction to R Stat 575 - Statistical Computing - Summer 2016 Dr. Junvie Pailden Southern Illinois University Edwardsville July 5, 2016 Why R R offers a powerful and appealing interactive environment

More information

R Programming: Worksheet 3

R Programming: Worksheet 3 R Programming: Worksheet 3 By the end of the practical you should feel confident writing and calling functions, and using if(), for() and while() constructions. 1. Review (a) Write a function which takes

More information

Statistical Programming with R

Statistical Programming with R Statistical Programming with R Lecture 9: Basic graphics in R Part 2 Bisher M. Iqelan biqelan@iugaza.edu.ps Department of Mathematics, Faculty of Science, The Islamic University of Gaza 2017-2018, Semester

More information

An introduction to Scheme

An introduction to Scheme An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

More information

Shell programming. Introduction to Operating Systems

Shell programming. Introduction to Operating Systems Shell programming Introduction to Operating Systems Environment variables Predened variables $* all parameters $# number of parameters $? result of last command $$ process identier $i parameter number

More information

Computer Programming: C++

Computer Programming: C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II Passing Array to a Function

More information

simpler Using R for Introductory Statistics

simpler Using R for Introductory Statistics John Verzani y 2e+05 4e+05 6e+05 8e+05 20000 40000 60000 80000 120000 160000 Preface page i These notes are an introduction to using the statistical software package R for an introductory statistics course.

More information

Statistics 120 Statistical Computing With R. First Prev Next Last Go Back Full Screen Close Quit

Statistics 120 Statistical Computing With R. First Prev Next Last Go Back Full Screen Close Quit Statistics 120 Statistical Computing With R First Prev Next Last Go Back Full Screen Close Quit The R System This course uses the R computing environment for practical examples. R serves both as a statistical

More information

Package reval. May 26, 2015

Package reval. May 26, 2015 Package reval May 26, 2015 Title Repeated Function Evaluation for Sensitivity Analysis Version 2.0.0 Date 2015-05-25 Author Michael C Koohafkan [aut, cre] Maintainer Michael C Koohafkan

More information

DSCI 325: Handout 24 Introduction to Writing Functions in R Spring 2017

DSCI 325: Handout 24 Introduction to Writing Functions in R Spring 2017 DSCI 325: Handout 24 Introduction to Writing Functions in R Spring 2017 We have already used several existing R functions in previous handouts. For example, consider the Grades dataset. Once the data frame

More information

Chapter 6: User defined functions and function files

Chapter 6: User defined functions and function files The Islamic University of Gaza Faculty of Engineering Civil Engineering Department Computer Programming (ECIV 2302) Chapter 6: User defined functions and function files ١ 6.1 Creating a function file Input

More information

Functions and Objects. Week 7: Symbolic Computation

Functions and Objects. Week 7: Symbolic Computation Week 7: Symbolic Computation In the previous weeks, we've seen the essential elements of modern functional programming: Functions (rst class) Types (parametric) Pattern Matching Lists This week, we'll

More information

Behavior of the sample mean. varx i = σ 2

Behavior of the sample mean. varx i = σ 2 Behavior of the sample mean We observe n independent and identically distributed (iid) draws from a random variable X. Denote the observed values by X 1, X 2,..., X n. Assume the X i come from a population

More information

A Brief Introduction to R

A Brief Introduction to R A Brief Introduction to R Babak Shahbaba Department of Statistics, University of California, Irvine, USA Chapter 1 Introduction to R 1.1 Installing R To install R, follow these steps: 1. Go to http://www.r-project.org/.

More information

Stochastic Models. Introduction to R. Walt Pohl. February 28, Department of Business Administration

Stochastic Models. Introduction to R. Walt Pohl. February 28, Department of Business Administration Stochastic Models Introduction to R Walt Pohl Universität Zürich Department of Business Administration February 28, 2013 What is R? R is a freely-available general-purpose statistical package, developed

More information

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson Introduction to Functional Programming in Racket CS 550 Programming Languages Jeremy Johnson 1 Objective To introduce functional programming in racket Programs are functions and their semantics involve

More information

Univariate Data - 2. Numeric Summaries

Univariate Data - 2. Numeric Summaries Univariate Data - 2. Numeric Summaries Young W. Lim 2018-08-01 Mon Young W. Lim Univariate Data - 2. Numeric Summaries 2018-08-01 Mon 1 / 36 Outline 1 Univariate Data Based on Numerical Summaries R Numeric

More information

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires 1 A couple of Functions 1 Let's take another example of a simple lisp function { one that does insertion sort. Let us assume that this sort function takes as input a list of numbers and sorts them in ascending

More information

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian

More information

Variables. Substitution

Variables. Substitution Variables Elements of Programming Languages Lecture 4: Variables, binding and substitution James Cheney University of Edinburgh October 6, 2015 A variable is a symbol that can stand for another expression.

More information

Lecture 27: Nov 26, S3 Programming. OOP S3 Objects Unpaired (Two Sample) t-test. James Balamuta STAT UIUC

Lecture 27: Nov 26, S3 Programming. OOP S3 Objects Unpaired (Two Sample) t-test. James Balamuta STAT UIUC Lecture 27: Nov 26, 2018 S3 Programming OOP S3 Objects Unpaired (Two Sample) t-test James Balamuta STAT 385 @ UIUC Announcements Group Project Final Report, Demo Video, and Peer Evaluation due Tuesday,

More information

A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

More information

R Programming: Worksheet 6

R Programming: Worksheet 6 R Programming: Worksheet 6 Today we ll study a few useful functions we haven t come across yet: all(), any(), `%in%`, match(), pmax(), pmin(), unique() We ll also apply our knowledge to the bootstrap.

More information

Bjørn Helge Mevik Research Computing Services, USIT, UiO

Bjørn Helge Mevik Research Computing Services, USIT, UiO 23.11.2011 1 Introduction to R and Bioconductor: Computer Lab Bjørn Helge Mevik (b.h.mevik@usit.uio.no), Research Computing Services, USIT, UiO (based on original by Antonio Mora, biotek) Exercise 1. Fundamentals

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 6: User-Defined Functions I In this chapter, you will: Objectives Learn about standard (predefined) functions and discover

More information

Chapter 15 Functional Programming Languages

Chapter 15 Functional Programming Languages Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions.

More information

R Primer for Introduction to Mathematical Statistics 8th Edition Joseph W. McKean

R Primer for Introduction to Mathematical Statistics 8th Edition Joseph W. McKean R Primer for Introduction to Mathematical Statistics 8th Edition Joseph W. McKean Copyright 2017 by Joseph W. McKean at Western Michigan University. All rights reserved. Reproduction or translation of

More information

Statistics 406 Exam November 17, 2005

Statistics 406 Exam November 17, 2005 Statistics 406 Exam November 17, 2005 1. For each of the following, what do you expect the value of A to be after executing the program? Briefly state your reasoning for each part. (a) X

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and

More information

Lecture 3: Basics of R Programming

Lecture 3: Basics of R Programming Lecture 3: Basics of R Programming This lecture introduces how to do things with R beyond simple commands. We will explore programming in R. What is programming? It is the act of instructing a computer

More information

What is Matlab? The command line Variables Operators Functions

What is Matlab? The command line Variables Operators Functions What is Matlab? The command line Variables Operators Functions Vectors Matrices Control Structures Programming in Matlab Graphics and Plotting A numerical computing environment Simple and effective programming

More information

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation 1 MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { November 12,

More information

Lecture 9 - C Functions

Lecture 9 - C Functions ECET 264 C Programming Language with Applications Lecture 9 C Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 9- Prof. Paul I. Lin

More information

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp. Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also

More information

mmpf: Monte-Carlo Methods for Prediction Functions by Zachary M. Jones

mmpf: Monte-Carlo Methods for Prediction Functions by Zachary M. Jones CONTRIBUTED RESEARCH ARTICLE 1 mmpf: Monte-Carlo Methods for Prediction Functions by Zachary M. Jones Abstract Machine learning methods can often learn high-dimensional functions which generalize well

More information

An Introduction to Statistical Computing in R

An Introduction to Statistical Computing in R An Introduction to Statistical Computing in R K2I Data Science Boot Camp - Day 1 AM Session May 15, 2017 Statistical Computing in R May 15, 2017 1 / 55 AM Session Outline Intro to R Basics Plotting In

More information

Writing Functions! Part I!

Writing Functions! Part I! Writing Functions! Part I! In your mat219_class project 1. Create a new R script or R notebook called wri7ng_func7ons 2. Include this code in your script or notebook: library(tidyverse) library(gapminder)

More information

www.thestudycampus.com Methods Let s imagine an automobile factory. When an automobile is manufactured, it is not made from basic raw materials; it is put together from previously manufactured parts. Some

More information

Lecture 3: Basics of R Programming

Lecture 3: Basics of R Programming Lecture 3: Basics of R Programming This lecture introduces you to how to do more things with R beyond simple commands. Outline: 1. R as a programming language 2. Grouping, loops and conditional execution

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages. Lecture 16 CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

More information

SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Chapter 1. Fundamentals of Higher Order Programming

Chapter 1. Fundamentals of Higher Order Programming Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

More information

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science An Interactive Desk Calculator Project P2 of Common Lisp: An Interactive Approach Stuart C. Shapiro Department of Computer Science State University of New York at Bualo January 25, 1996 The goal of this

More information

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions C++ Programming Language Lab # 6 Functions Objective: To be familiar with

More information

Numeric Vectors STAT 133. Gaston Sanchez. Department of Statistics, UC Berkeley

Numeric Vectors STAT 133. Gaston Sanchez. Department of Statistics, UC Berkeley Numeric Vectors STAT 133 Gaston Sanchez Department of Statistics, UC Berkeley gastonsanchez.com github.com/gastonstat/stat133 Course web: gastonsanchez.com/stat133 Data Types and Structures To make the

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

More information

Midterm Examination , Fall 2011

Midterm Examination , Fall 2011 Midterm Examination 36-350, Fall 2011 Instructions: Provide all answers in your bluebook. Only work in the bluebook will be graded. Clearly indicate which problem each answer goes with. There are 100 points

More information

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones. 6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

05R03 Control Structure and Functions

05R03 Control Structure and Functions R Programming Language R03 Control Structure 林 C.F. Jeff Lin, MD. PhD. Control Structures and Functions 北 理 北 Jeff Lin, MD. PhD. 1 Jeff Lin, MD. PhD. 2 # semicolons > x

More information

Programming with R. Bjørn-Helge Mevik. RIS Course Week spring Research Infrastructure Services Group, USIT, UiO

Programming with R. Bjørn-Helge Mevik. RIS Course Week spring Research Infrastructure Services Group, USIT, UiO Programming with R Bjørn-Helge Mevik Research Infrastructure Services Group, USIT, UiO RIS Course Week spring 2014 Bjørn-Helge Mevik (RIS) Programming with R Course Week spring 2014 1 / 27 Introduction

More information

An Introduction to Functions

An Introduction to Functions Chapter 4 An Introduction to Functions Through the agency of with, we have added identifiers and the ability to name expressions to the language. Much of the time, though, simply being able to name an

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More information

INF121: Functional Algorithmic and Programming

INF121: Functional Algorithmic and Programming INF121: Functional Algorithmic and Programming Lecture 2: Identifiers and functions Academic Year 2011-2012 Identifiers The notion of identifier A fundamental concept of programming languages: associating

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 Glasgow Haskell Compiler GHC is the leading implementation of Haskell, and comprises a compiler and interpreter; The interactive nature of the interpreter

More information

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G. Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

More information

Parsing Scheme (+ (* 2 3) 1) * 1

Parsing Scheme (+ (* 2 3) 1) * 1 Parsing Scheme + (+ (* 2 3) 1) * 1 2 3 Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply + Compiling Scheme make-return START make-test make-close make-assign make- pair? yes

More information

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,

More information

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter 6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

EPIB Four Lecture Overview of R

EPIB Four Lecture Overview of R EPIB-613 - Four Lecture Overview of R R is a package with enormous capacity for complex statistical analysis. We will see only a small proportion of what it can do. The R component of EPIB-613 is divided

More information

Programming R. Manuel J. A. Eugster. Chapter 4: Writing your own functions. Last modification on May 15, 2012

Programming R. Manuel J. A. Eugster. Chapter 4: Writing your own functions. Last modification on May 15, 2012 Manuel J. A. Eugster Programming R Chapter 4: Writing your own functions Last modification on May 15, 2012 Draft of the R programming book I always wanted to read http://mjaeugster.github.com/progr Licensed

More information

User Guide.

User Guide. User Guide Learn more about graphing functions, plotting tables of data, evaluating equations, exploring transformations, and more! If you have questions that aren t answered in here, send us an email

More information

Business Statistics: R tutorials

Business Statistics: R tutorials Business Statistics: R tutorials Jingyu He September 29, 2017 Install R and RStudio R is a free software environment for statistical computing and graphics. Download free R and RStudio for Windows/Mac:

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns Previous lecture Basics of Scheme Expressions and associated values (or syntax and

More information

COMP 208 Computers in Engineering

COMP 208 Computers in Engineering COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP 208 - Lecture 14 1 Review: basics of C C is case sensitive 2 types of comments: /* */,

More information

Turtles All The Way Down

Turtles All The Way Down Turtles All The Way Down Bertrand Russell had just finished giving a public lecture on the nature of the universe. An old woman said Prof. Russell, it is well known that the earth rests on the back of

More information

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages CSc 520 Principles of Programming Languages 3: Scheme Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

More information

CS1101: Lecture 9 The Shell as a Programming Language

CS1101: Lecture 9 The Shell as a Programming Language CS1101: Lecture 9 The Shell as a Programming Language Dr. Barry O Sullivan b.osullivan@cs.ucc.ie Counting Arguments Using read Lecture Outline Arithmetic using expr Course Homepage http://www.cs.ucc.ie/

More information

CSE 341 Section Handout #6 Cheat Sheet

CSE 341 Section Handout #6 Cheat Sheet Cheat Sheet Types numbers: integers (3, 802), reals (3.4), rationals (3/4), complex (2+3.4i) symbols: x, y, hello, r2d2 booleans: #t, #f strings: "hello", "how are you?" lists: (list 3 4 5) (list 98.5

More information

An Introduction to Scheme

An Introduction to Scheme An Introduction to Scheme Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Stéphane Ducasse 1 Scheme Minimal Statically scoped Functional Imperative Stack manipulation Specification

More information

Scheme: Strings Scheme: I/O

Scheme: Strings Scheme: I/O Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of

More information

Functional Programming Lecture 1: Introduction

Functional Programming Lecture 1: Introduction Functional Programming Lecture 1: Introduction Viliam Lisý Artificial Intelligence Center Department of Computer Science FEE, Czech Technical University in Prague viliam.lisy@fel.cvut.cz Acknowledgements

More information

Chapter 3: Programming with MATLAB

Chapter 3: Programming with MATLAB Chapter 3: Programming with MATLAB Choi Hae Jin Chapter Objectives q Learning how to create well-documented M-files in the edit window and invoke them from the command window. q Understanding how script

More information

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; } Ex: The difference between Compiler and Interpreter The interpreter actually carries out the computations specified in the source program. In other words, the output of a compiler is a program, whereas

More information

MAS115 R programming, Lab Class 5 Prof. P. G. Blackwell

MAS115 R programming, Lab Class 5 Prof. P. G. Blackwell MAS115 R programming, Lab Class 5 Prof. P. G. Blackwell 2017-18 1 Writing functions 1.1 Introduction to Functions R has a lot of built in functions that you have already used e.g. sum(), nrow(), runif(),....

More information

Decaf Language Reference Manual

Decaf Language Reference Manual Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented

More information

Lecture 13: Jul 5, Functions. Background Asserting Input Debugging Errors. James Balamuta STAT UIUC

Lecture 13: Jul 5, Functions. Background Asserting Input Debugging Errors. James Balamuta STAT UIUC Lecture 13: Jul 5, 2018 Functions Background Asserting Input Debugging Errors James Balamuta STAT 385 @ UIUC Announcements hw04 will be due Tuesday, July 10th, 2018 Project Proposal is due on Friday, July

More information

9. Writing Functions

9. Writing Functions 9. Writing Functions Ken Rice Thomas Lumley Universities of Washington and Auckland NYU Abu Dhabi, January 2017 In this session One of the most powerful features of R is the user s ability to expand existing

More information

Lecture 3 - Object-oriented programming and statistical programming examples

Lecture 3 - Object-oriented programming and statistical programming examples Lecture 3 - Object-oriented programming and statistical programming examples Björn Andersson (w/ Ronnie Pingel) Department of Statistics, Uppsala University February 1, 2013 Table of Contents 1 Some notes

More information

Prewitt. Gradient. Image. Op. Merging of Small Regions. Curve Approximation. and

Prewitt. Gradient. Image. Op. Merging of Small Regions. Curve Approximation. and A RULE-BASED SYSTEM FOR REGION SEGMENTATION IMPROVEMENT IN STEREOVISION M. Buvry, E. Zagrouba and C. J. Krey ENSEEIHT - IRIT - UA 1399 CNRS Vision par Calculateur A. Bruel 2 Rue Camichel, 31071 Toulouse

More information

Functions of Two Variables

Functions of Two Variables Functions of Two Variables MATLAB allows us to work with functions of more than one variable With MATLAB 5 we can even move beyond the traditional M N matrix to matrices with an arbitrary number of dimensions

More information

Statistical Programming Worksheet 4

Statistical Programming Worksheet 4 Statistical Programming Worksheet 4 1. Cholesky Decomposition. (a) Write a function with argument n to generate a random symmetric n n-positive definite matrix. To do this: generate an n n matrix C whose

More information

Lecture 7. SchemeList, finish up; Universal Hashing introduction

Lecture 7. SchemeList, finish up; Universal Hashing introduction Lecture 7. SchemeList, finish up; Universal Hashing introduction CS 16 February 24, 2010 1 / 15 foldleft #!/usr/bin/python def foldleft(func, slist, init): foldleft: ( * -> ) * ( SchemeList)

More information

R Workshop Daniel Fuller

R Workshop Daniel Fuller R Workshop Daniel Fuller Welcome to the R Workshop @ Memorial HKR The R project for statistical computing is a free open source statistical programming language and project. Follow these steps to get started:

More information

16 Greedy Algorithms

16 Greedy Algorithms 16 Greedy Algorithms Optimization algorithms typically go through a sequence of steps, with a set of choices at each For many optimization problems, using dynamic programming to determine the best choices

More information

Modeling and Simulating Social Systems with MATLAB

Modeling and Simulating Social Systems with MATLAB Modeling and Simulating Social Systems with MATLAB Lecture 2 Statistics and Plotting in MATLAB Olivia Woolley, Tobias Kuhn, Dario Biasini, Dirk Helbing Chair of Sociology, in particular of Modeling and

More information

Week 6: Introduction to Programming for GIS and Spatial Data Analysis GEO GEO A

Week 6: Introduction to Programming for GIS and Spatial Data Analysis GEO GEO A Week 6: Modules Procedures and Functions Introduction to Programming for GIS and Spatial Data Analysis GEO4938 1469 GEO6938 147A Review: Sequences of Instructions Strict sequential flow: Step One Two Three

More information

Lecture 02 The Shell and Shell Scripting

Lecture 02 The Shell and Shell Scripting Lecture 02 The Shell and Shell Scripting In this course, we need to be familiar with the "UNIX shell". We use it, whether bash, csh, tcsh, zsh, or other variants, to start and stop processes, control the

More information

Recap: Functions as first-class values

Recap: Functions as first-class values Recap: Functions as first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse

More information

Common LISP Tutorial 1 (Basic)

Common LISP Tutorial 1 (Basic) Common LISP Tutorial 1 (Basic) CLISP Download https://sourceforge.net/projects/clisp/ IPPL Course Materials (UST sir only) Download https://silp.iiita.ac.in/wordpress/?page_id=494 Introduction Lisp (1958)

More information

Appendix A: A Sample R Session

Appendix A: A Sample R Session Appendix A: The purpose of this appendix is to become accustumed to the R and the way if responds to line commands. As you progress, you will learn the statistical ideas behind the commands that ask for

More information

Introduction to Software Testing Chapter 2.3 Graph Coverage for Source Code. Paul Ammann & Jeff Offutt

Introduction to Software Testing Chapter 2.3 Graph Coverage for Source Code. Paul Ammann & Jeff Offutt Introduction to Software Testing Chapter 2.3 Graph Coverage for Source Code Paul Ammann & Jeff Offutt Overview The most common application of graph criteria is to program source Graph : Usually the control

More information