Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia.

Slides:



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

Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
 Statistics package  Graphics package  Programming language  Can be used to share/reproduce analyses  Many new packages being created - can be downloaded.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Introduction to GTECH 201 Session 13. What is R? Statistics package A GNU project based on the S language Statistical environment Graphics package Programming.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
3. Functions and Arguments. Writing in R is like writing in English Jump three times forward Action Modifiers.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Python Programming Fundamentals
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Piotr Wolski Introduction to R. Topics What is R? Sample session How to install R? Minimum you have to know to work in R Data objects in R and how to.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Lecture 7 Sept 22 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
CSE123 Lecture 3 Files and File ManagementScripts.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Lecture 11 Introduction to R and Accessing USGS Data from Web Services Jeffery S. Horsburgh Hydroinformatics Fall 2013 This work was funded by National.
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Information and Computer Sciences University of Hawaii, Manoa
Sihua Peng, PhD Shanghai Ocean University
Chapter 7 User-Defined Methods.
Scripts & Functions Scripts and functions are contained in .m-files
Agenda Control Flow Statements Purpose test statement
11/10/2018.
Chapter 7 Files and Exceptions
Introduction to Python
Homework Any Questions?.
R Course 1st Lecture.
Chapter 5 R programming Instructor: Li, Han.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Statistical Software An introduction to Statistics Using R Instructed by Jinzhu Jia

Chap 2. Programming with R Workspace Working directory Input and output Flow control Functions Packages Writing R scripts

Workspace All of the objects are the content of the R workspace What objects do you have in your R workspace ?  objects()  ls()  ls.str()  library(lsr) ## Note: the first time we see using a package  who() ## Anytime you want to use who(), please use the command ``library(lsr)” first. Will learn more later  Differences?

Removing variables rm(X,Y) ## variabes X and Y will be removed from the workspace rm(list = c(‘X’,’Y’)) ## the same as the above command rm(list = ls())

Working directory Working directory specifies what folder your data will be read from or save to. getwd() ## wd denotes working directory setwd(‘./newfolder’) ##./ denotes the current folder You can also use Misc->Changing working directory to choose your folder in your MAC list.files() ## list all the files in the current wd.

Reading data read.csv() ## most frequently used read.table() ## frequently used scan() ## readLines() ## data() ## R has a few data sets in its packages ## this function lists dataset names stored in the workspace, such as data(‘data set name’) ## loads the data Try: data() data(Airpasse nengers) ls()

Saving data write.csv() write.table() save() ## save a few variables save.image() ## save the whole workspace load() See help for the details

R script It is a text file You can use it to write all your R commands Write your own package File->New Scrip ## you will get an empty script file source(rscipt_File)

Flow Control while loop while ( CONDITION ) { STATEMENT1 STATEMENT2 ETC } Note: { } is a block of statements; if you do not use {}, only the first statement will be executed in the loop

for loop for ( VAR in VECTOR ) { STATEMENT1 STATEMENT2 ETC } break() ## the same as in C next() ## C uses continue()

Conditional statements if ( CONDITION ) { STATEMENT1 STATEMENT2 ETC } switch() if ( CONDITION ) { STATEMENT1 STATEMENT2 ETC } else { STATEMENT3 STATEMENT4 ETC }

Rule of switch() switch(ExpR,….) Expr: an expression evaluating to a number or a character string If the value of EXPR is not a character string it is coerced to integer. The corresponding element of …. will be evaluated If EXPR evaluates to a character string then that string is matched (exactly)to the names of the elements in....

Functions FNAME <- function ( ARG1, ARG2, ETC ) { STATEMENT1 STATEMENT2 ETC return( VALUE ) }

Binary operator %*% ## matrix multiplication % ## modulus 5%2 = 1 %/% ## integer division 5%/%2 = 1 %in% ## ‘a’ %in% c(‘a’,’b’,’c’) = T Create your own binary operator ‘%+%’ <- function(s1,s2){}

Default parameters ? Seq seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL,...) We see a few parameters in function seq(), each of wich has default parameters. Example: mysub = function(x,y=1){z = x-y} mysub(2) = mysub(2,1) = mysub(x=2)

Recursive function Example: factorial <- function(x){ if(x==1) return( 1) else return(x*factorial(x-1)) }

Packages A package is a collection of objects It usually completes one special task Use a package: libray(‘package name’) Install a package: install.packages(‘package name’)

Homework Find all of the pairs of twin primes less than 10^6 using R and report the total number. Print a calendar for any given month and year as input. For example: mycal(2014,3) will give you a table like this: Send your code and results to Hint: ? as.Date() Help: