Introduction to R. Control Structures. Programming in R. Branching/Conditional statements: the if statement. example: if(...) {... } else {...



Similar documents
Lecture 2 Notes: Flow of Control

VB.NET Programming Fundamentals

Keil C51 Cross Compiler

Bash shell programming Part II Control statements

I PUC - Computer Science. Practical s Syllabus. Contents

Statements and Control Flow

Sources: On the Web: Slides will be available on:

Example of a Java program

Introduction to Java

Pseudo code Tutorial and Exercises Teacher s Version

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Moving from CS 61A Scheme to CS 61B Java

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

J a v a Quiz (Unit 3, Test 0 Practice)

Lecture Notes on Linear Search

Two-way selection. Branching and Looping

Curriculum Map. Discipline: Computer Science Course: C++

TIP: To access the WinRunner help system at any time, press the F1 key.

Chapter 15 Functional Programming Languages

Object Oriented Software Design

Unit 6. Loop statements

R Language Definition

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Oracle Database: SQL and PL/SQL Fundamentals

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Oracle 10g PL/SQL Training

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Oracle Database: SQL and PL/SQL Fundamentals

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

PHP Tutorial From beginner to master

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

CS 3719 (Theory of Computation and Algorithms) Lecture 4

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

Systems Programming & Scripting

Java from a C perspective. Plan

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Iteration CHAPTER 6. Topic Summary

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

CmpSci 187: Programming with Data Structures Spring 2015

ChE-1800 H-2: Flowchart Diagrams (last updated January 13, 2013)

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Analysis of Binary Search algorithm and Selection Sort algorithm

WAYNESBORO AREA SCHOOL DISTRICT CURRICULUM INTRODUCTION TO COMPUTER SCIENCE (June 2014)

Introduction to Matlab

Package EstCRM. July 13, 2015

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

In this Chapter you ll learn:

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Oracle Database: SQL and PL/SQL Fundamentals NEW

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

GRID SEARCHING Novel way of Searching 2D Array

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

JavaScript: Arrays Pearson Education, Inc. All rights reserved.

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

A few useful MATLAB functions

Tutorial on C Language Programming

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Section IV.1: Recursive Algorithms and Recursion Trees

Python for Rookies. Example Examination Paper

Dataframes. Lecture 8. Nicholas Christian BIOST 2094 Spring 2011

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Oracle SQL. Course Summary. Duration. Objectives

CS106A, Stanford Handout #38. Strings and Chars

if and if-else: Part 1

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Moving from C++ to VBA

Using Casio Graphics Calculators

Answers to Review Questions Chapter 7

Introduction to Programming (in C++) Multi-dimensional vectors. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

JavaScript Introduction

Computer Programming I

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Selection Statements

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

An Incomplete C++ Primer. University of Wyoming MA 5310

MAT 200, Midterm Exam Solution. a. (5 points) Compute the determinant of the matrix A =

Baseline Question Types and Report Outcomes November 7, 2014

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Symbol Tables. Introduction

Sequence Diagram Tutorial. From: UML Distilled, Third Edition, Chapter 4 M. Fowler

Course Title: Software Development

CS193j, Stanford Handout #4. Java 2

PL / SQL Basics. Chapter 3

1.3 Conditionals and Loops

Technical University of Sofia Faculty of Computer Systems and Control. Web Programming. Lecture 4 JavaScript

Advanced Bash Scripting. Joshua Malone

Writing Control Structures

Computer Science 210: Data Structures. Searching

RSA Question 2. Bob thinks that p and q are primes but p isn t. Then, Bob thinks Φ Bob :=(p-1)(q-1) = φ(n). Is this true?

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Transcription:

Introduction to R Programming in R 1 Control Structures Branching/Conditional statements: the if statement example: if(...) {... else {... if(x < 0){ cat( negative ) else{ cat( non-negative ) 2

The switch statement 4 Control Structures Conditional Statements logical comparisons binary operators operator meaning == equality < less than > greater than <= >= less than or equal to greater than or equal to operator &&! meaning AND both statements TRUE = TRUE OR at least one statement TRUE = TRUE NOT statement is FALSE = TRUE 3 Control Structures: Conditional Statements Using an if statement in an assignment: The ifelse function This function has the form ifelse(test, yes, no) x <- if(...) 3.14 else 2.71 test is the logical condition, yes is what iftest() will return if the logical condition test is true, and no is what iftest() returns when false. example: x <- rnorm(100) y <- ifelse(x>0, 1, -1) z <- ifelse(x>0, 1, ifelse(x<0, -1, 0)) When x is positive, y & z store 1. When x is negative y stores -1 and z stores -1. When x = 0, y stores -1 and z stores 0.

Control Structures: Iteration and Looping The for loop: do something for all items in a vector or list. general syntax: here we iterate over the items in a vector (or list) for (item in a_vector) { #operate on item to iterate n times using a for loop, specify a vector of numbers 1:n in the for loop: for (index in 1:100) { #do something 100 times 5 Control Structures: Iteration and Looping The while loop: do something while a logical statement is true. general syntax: here we loop until statement is false example: loop so long as x > 0 while (statement) { #do something x <- 100 while (x > 0) { a <- runif(1, 1, 10) #do something x <- x - a 6

The repeat statement: Control Structures: Iteration and Looping repeats the block of code until a break statement is reached general syntax: repeats until statement is true, when break is called example: repeat until x < 0 repeat { #do something if(statement) { break repeat { x <- rnorm(1, 0, 1) # do something if(x < 0){ break note: the break command always terminates a loop 7 lapply, sapply, and apply lapply Control Structures: Iteration and Looping These functions can be used when the same or similar tasks need to be performed multiple times for all elements of a vector, list, or columns of an array. Easier and faster than a for loop. Usage: lapply(alist, afunction) - applies afunction to all the elements of a list or vector, returns a list of the results. cap.state.name <- lapply(state.name, toupper) cap.state.name is a list of all the state names capitalized. 8

sapply Control Structures: Iteration and Looping apply Similar to lapply, but returns a simpler data structure: either a vector or array. Usage: apply(array/matrix, dim, afunction) Applies afunction to the dim dimension of matrix or array. Returns a vector. > x [,1] [,2] [,3] [1,] 5 9 7 [2,] 7 8 6 [3,] 0 4 3 [4,] 7 6 5 > apply(x, 1, sum) [1] 21 21 7 18 > apply(x, 2, sum) [1] 19 27 21 9 Control Structures: Iteration and Looping Your Turn: Write a program that iterates over a vector of states names and stores the states that begin with the letter M R has a special vector for all the spaces names called state.name To get the first character of a string, use the substr(string, start, stop) function msg <- hello first_char_msg <- substr(msg, 0, 1) first_char_msg stores h 10

What are functions? Functions Functions are blocks of code that allows R to be modular and facilitate code reuse. An R programmer can define their own functions as follows: function_name <- function([arg1], [arg2],...){ #function code body The function arguments are optional. Function arguments are the variables passed to the function, used by the function s code to perform calculations. A function can take no arguments. A function can also return any R primitive or object using the return(object) statement. 11 Functions Examples: # computes the mean of a vector of numbers mean <- function(a_vector) { s <- sum(a_vector) x <- s/length(a_vector) # checks to see if a string s starts with letter x startswith <- function(x, s){ if(x == substr(s, 0, 1){ return(true) return(false) 12

Default arguments Functions Function arguments can be assigned default values as follows: sort_vector <- function(a_vector, ascending=true){ # sorting algorithm when calling this function, the arguments given default values do not need to be specified. In this case, the defaults are used. sort_vector(a_vector) # returns a_vector in ascending order sort_vector(a_vector, FALSE) # returns a vector in descending order 13 Variable Scoping Functions Variables that are bound to an R primitive or object outside a function are called global variables, and are accessible everywhere in an R program. x <- 5 test <- function() { cat(x + 5) test(); #prints out 25 14

Functions variable scoping Variables bound inside a function are only accessible within that function. These are called local variables. x <- 10 test <- function() { x <- 5 cat(x + 20) test() #prints 25 cat(x + 20) #prints 30 Note that the local variable assignment takes precedence inside the function test over the global assignment. 15 Functions variable scoping R functions have no side effects-- they cannot change the value of global variables x <- 10 test <- function(z) { z <- z + 10 cat(z) test(x) #prints 20 cat(x) #prints 10 Note that x is still bound to 10 outside of test(). 16

Advanced Function Topics Functions can be declared and used inside a function test <- function(y) { square <- function(x) { return(x*x) cube <- function(x) {return(x^3) return(square(x) + cube(x)) cat(test(4)) #prints 80 17 Advanced Function Topics Functions in R can also return function. test <- function(y) { return(function(z) {y + z) a <- test(10) cat(a(4)) # prints 14 b <- test(2) cat(b(4)) #prints 6 Note that the y in the returned function gets bound to the argument passed and never is never changed. Thus the variables in this function is closed inside its local environment. These types of constructs are referred to as closures. 18

Functions Your Turn: Write a function that computes the standard deviation of a vector of numbers. Then write a function that determines whether a string contains the letter s. 19 Intro to Simulation R is an excellent environment to do simulations. The programming techniques previously learned can be used to do simulations. In this section we cover simulation by example: dealing cards. 20

Intro to Simulation dealing cards First we need some data to work on, in this case a deck of cards. suit <- c("h","c","d", "S") rank <- c(2:9, "T", "J", "Q", "K", "A") deck <- NULL #create the deck for(r in rank){ deck <- c(deck, paste(r, suit)) 21 Intro to Simulation dealing cards Next we need to write functions to work on the deck of cards: #returns a randomly shuffled deck shuffle <- function(deck) { return(sample(deck,length(deck))) #draws n cards from deck starting at card start draw_cards <- function(deck, start, n=1) { cards <- NULL for(i in start:(start + n - 1)){ if(i <= length(deck)){ cards <- c(cards, deck[i]) return(cards) 22

We can simulate drawing cards, e.g. card games like poker. We can ask questions like What is the probability of drawing 2 cards of the same suit from a deck of 52 cards? By simulating the drawing of 2 cards from a large number of randomly shuffled decks. These simulations are known as Monte Carlo simulations. #checks to see it 2 cards are of the same suit same_suit <- function(card1, card2) { return(substr(card1, 3,3) == substr(card2, 3,3)) #simulation sscount <- 0 iterations <- 0 while(iterations < 10000) #do 10000 simulations { sdeck <- shuffle(create_deck()) twocards <- draw_cards(sdeck, n=2) if(same_suit(twocards[1], twocards[2])){ sscount <- sscount + 1 iterations <- iterations + 1 p <- sscount/iterations #the estimated probability of 2 cards same suit cat(p) 23 Simulation Your Turn: Use Monte Carlo simulation to determine the probability of drawing 2 cards of the same rank. 24