1 R Programming Zhi Wei. 2 Language layout  Three types of statement expression: it is evaluated, printed, and the value is lost (3+5) assignment: passes.

Slides:



Advertisements
Similar presentations
Introduction to R Brody Sandel. Topics Approaching your analysis Basic structure of R Basic programming Plotting Spatial data.
Advertisements

An Introduction to R: Logic & Basics. The R language Command line Can be executed within a terminal Within Emacs using ESS (Emacs Speaks Statistics)
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Using Unix Shell Scripts to Manage Large Data
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Ch 111 Chapter 11 Advanced Batch Files. Ch 112 Overview This chapter focuses on batch file commands that allow you to:  write sophisticated batch files.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Excel 4 Conditional Functions, Formats, and Backstage.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
732A44 Programming in R.  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of.
Lesson 6. GCSE Computing – programming languages Candidates should be able to:  describe common tools and facilities available in an integrated development.
Intro to R Zhi Wei.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Just as there are many human languages, there are many computer programming languages that can be used to develop software. Some are named after people,
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
read and learn from example loop programs develop modular program
Intermediate 2 Computing Unit 2 - Software Development Topic 2 - Software Development Languages and Environments.
TCL TK. Tcl/Tk C functions can become Tcl commands that are invoked interactively Tk = scriptable, portable user interface –Windows, X (Unix), MacOS,
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1 Introduction to SQL *Plus Oracle SQL Interface MIS309 Database Systems.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Functions Commands Programming Steps Examples Printing Homework Hints.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Control Structures Hara URL:
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
CHAPTER 4 DECISIONS & LOOPS
CIRC Winter Boot Camp 2017 Baowei Liu
Using Unix Shell Scripts to Manage Large Data
Programming in R Intro, data and programming structures
Matlab Training Session 4: Control, Flow and Functions
Fonts, Pictures, Styles, Files, Spelling, and More!
JavaScript Loops.
Scripts & Functions Scripts and functions are contained in .m-files
Expressions and Control Flow in JavaScript
Matlab Workshop 9/22/2018.
Introduction to Python
Matlab review Matlab is a numerical analysis system
Exploring Microsoft Excel
CS100J 26 April. Matlab Use help button!!! Variables, values, types
Use of Mathematics using Technology (Maltlab)
Recoding III: Introducing apply()
MATLAB Tutorial Dr. David W. Graham.
Recoding III: Introducing apply()
Exam 1 Material Study Guide
Computer Science Core Concepts
CSCI N317 Computation for Scientific Applications Unit R
Ch 9-10 Subsetting Ordering Array operations Iteration
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CSCI N207 Data Analysis Using Spreadsheet
1-6 Midterm Review.
The Python interpreter
Programming The ideal style of programming is Structured or
Presentation transcript:

1 R Programming Zhi Wei

2 Language layout  Three types of statement expression: it is evaluated, printed, and the value is lost (3+5) assignment: passes the value to a variable but the result is not printed automatically (out<- 3+5) comment: (#This is a comment)

3 Loops and conditionals  Conditional if (expr) expr if (expr) expr else expr  Iteration repeat expr while (expr) expr for (name in expr1) expr  For comparisons use: equal: == not equal: != greater/less than: > < greater/less than or equal: >= <= NOT, AND, OR: !, &, |

4 Examples  What does the following code do? if (value==1) {check<-1} else {check<-0} counter<-0 for (i in 1:10) { if (geneExpr[i,2]<10){counter<-counter+1} } counter=sum(geneExpr[c(1:10),2]<10)

Accessing a data frame with logical vector data set ewr (UsingR package) >attach(ewr) >boxplot(ewr[inorout=="in", 3:10], main="Taxi in") >detach(ewr)  subset() function new.df = subset(old.df, subset=…, select=…) ewr.in = subset(ewr, subset= inorout=="in", select=3:10) ewr.out = subset(ewr, subset= inorout=="out", select=3:10) 5

Sorting a data frame by one of its column >attach(mtcars) >mtcars[order(mpg),] >mtcars[order(mpg, decreasing=T),] >mtcars[order(cyl,hp),] 6

Vectorization: avoid Loop (page 26, supp. R book, moodle has a copy)  apply(X, MARGIN, FUN,...) find minimum values of each row of matrix m  m <- matrix(rnorm(100), ncol=4)  apply(m, 2, min) ##- function with extra args:  myMin cut]) }  apply(m,2, myMin, cut=0)  lapply(X, FUN,...), sapply, tapply lapply(thuesen, mean, na.rm=T) sapply(thuesen, mean, na.rm=T) tapply(energy$expend, energy$stature, median) aggregate(energy$expend, energy["stature"], median) 7

Edit and Save your script  Can write many lines of code in any of your favorite text editors and run all at once Simply paste the commands into R Save your script and then Use function source( “ path/yourscript ” ), to run in batch mode the functions/codes saved in file “ yourscript ” (use options(echo=T) to have the commands echoed) 8