Programming in R Intro, data and programming structures

Slides:



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

R for Macroecology Aarhus University, Spring 2011.
Training on R For 3 rd and 4 th Year Honours Students, Dept. of Statistics, RU Empowered by Higher Education Quality Enhancement Project (HEQEP) Department.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Lecture 2 LISAM. Statistical software.. LISAM What is LISAM? Social network for Creating personal pages Creating courses  Storing course materials (lectures,
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Training on R-language Mārtiņš Liberts Central Statistical Bureau of Latvia.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
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.
Data, graphics, and programming in R 28.1, 30.1, Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
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.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Using the R software R is an open source comprehensive statistical package, more and more used around the world. R project web site:
Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,
R Programming Yang, Yufei. Normal distribution.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
STAT 534: Statistical Computing Hari Narayanan
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Lecture 11 Introduction to R and Accessing USGS Data from Web Services Jeffery S. Horsburgh Hydroinformatics Fall 2013 This work was funded by National.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
전자장 1 실험 - Matlab 사용법 - Photonic Systems Laboratory School of EE, Seoul National University Photonic Systems Lab School of EECS, S.N.U.
16BIT IITR Data Collection Module If you have not already done so, download and install R from download.
Working with data in R 2 Fish 552: Lecture 3. Recommended Reading An Introduction to R (R Development Core Team) –
Emdeon Office Batch Management Services This document provides detailed information on Batch Import Services and other Batch features.
PHP using MySQL Database for Web Development (part II)
Introduction to R.
ECE 1304 Introduction to Electrical and Computer Engineering
R programming language
Introduction to R Samal Dharmarathna.
LISAM. Statistical software.
L – Modeling and Simulating Social Systems with MATLAB
Introduction to Scripting
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Introduction to Python
Introduction to Python
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Matlab tutorial course
CIS16 Application Development and Programming using Visual Basic.net
PHP.
Statistics 540 Computing in Statistics
Communication and Coding Theory Lab(CS491)
Topics Sequences Introduction to Lists List Slicing
Introduction to Matlab
Basics of R, Ch Functions Help Managing your Objects
Matlab Intro.
INTRODUCTION TO MATLAB
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Functions continued.
R Course 1st Lecture.
Topics Sequences Introduction to Lists List Slicing
PHP an introduction.
Introduction to MATLAB
Matlab Intro.
Computer Simulation Lab
> Introduction to Nelson Rios, Tulane University
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Programming in R Intro, data and programming structures

About R R is an open source comprehensive statistical package, widely used around the world. R is a real object-oriented programming language (compared to SAS or Minitab). R is available for Windows, Mac and Linux. Can be compared to Matlab by structure, by origin – free version of S. + Easy and flexible programming. Has a lot of contributed packages covering most statistical and machine learning methods (also recent ones!). Slower than C and Fortran. Cannot handle as large data sets as for example Perl and Python. Programming in R

Installing R R project web site: http://www.r-project.org/ Programming in R

Programming in R

Starting R (Windows) Programming in R

Important menu items Set working directory alt. setwd("C:/…") Programming in R

Important menu items For script mode For inter- active mode Programming in R

Important menu items Run a script Programming in R

Important menu items Install contributed packages from CRAN Programming in R

Getting help Specific function Help browser help(function) Help browser help.start() Search for something in help help.search(“expression”) Quick reminder of function arguments: args(function) Examples of how to use function: example(function) If some method is not installed on the computer: RSiteSearch(”expression") Programming in R

Preliminaries R is case-sensitive Comments: Start with hash-mark #R is a cool language! Data assignment: Use -> or <- or = a <- 3 3 -> b c = 3 Variable types: called modes, ex. integer, numeric, character, logical, complex Programming in R

Working with vectors Vectors are the ‘workhorses’ of R The function c() combines individual values (comma-spaced) to a vector Print on screen by entering the variable name or use the function print() [1]is the row number, and in this case x is interpreted as a row vector The length of the vector is obtained with the length() function The mode of the vector is obtained with the mode() function Programming in R

Listing and removing objects Listing defined objects (vectors, matrices, data frames): Use the function ls() with no arguments Compact display of an R object. Useful for complex data frames and lists. Removing objects: Use the function rm() Programming in R

Sequences Use of ‘ : ‘ , seq() and rep() Programming in R

Indexing Finding elements satisfying specific conditions Indexing follows format vector1[vector2] Finding elements satisfying specific conditions Programming in R

Filtering Filtering follows the indexing principles Programming in R

Set operations on vectors Programming in R

Other operations on vectors Important: In R, operations with vectors are performed element- by-element Some operations: Element-wise: +-*/^ log exp sin cos sqrt length –number of elements sum - sum of all elements mean max min order Logicals: TRUE or FALSE: a<-TRUE; Programming in R

Working with matrices Use the function matrix() a<-matrix(values,nrow=m,ncol=n) values is a list of values enclosed in c(), i.e. a row vector or an already defined vector. m is the number of rows and n is the number of columns of the matrix. The number of values must be dividable by both m and n. The values are entered column-wise. The identifiers nrow= and ncol= can be omitted Note the double indexing, first number for row and second number for column Programming in R

Matrix filtering Follows the same principles as for vectors Column 2 Programming in R

Editing sizes of matrices Adding and deleting rows and columns of matrices Programming in R

Matrix operations Matrix/vector multiplication Matrix/matrix Programming in R

Matrix operations Matrix transpose b = aT Matrix inverse b = a-1 Programming in R

Lists A list is a collection of objects Can be of different modes (ex. "logical", "integer", "double", "character”) List indexing is different from vector and matrix indexing Programming in R

Lists Adding and deleting components of lists Programming in R

Lists Accessing list components and values unlist coerces to a common mode Programming in R

Data frames Data frames are two-dimensional analogs of matrices that can contain columns of different mode Use the function data.frame(object 1, object 2, … , object k) Matrices need to be protected , otherwise each column of a matrix will be identified as a single object in the data frame. Protection is made with the function I() Programming in R

Data frames Accessing components and values of data frames (both list and matrix indexing works) Programming in R

Data frames Combining data frames Programming in R

Factors A factor can be seen as a vector with certain levels Programming in R

Sorting Sorting according to either numeric values or characters Programming in R

Conditional execution if (expr) { … } else{ If you need to connect several conditions, use ’&’ , ’&&’, ’| ’ or ’||’ Programming in R

Loops for (name in expr1 ) { … } while (condition) Programming in R

Avoiding loops The *apply() family of functions is among the most important in R for speeding up computations apply(m,dimcode,f,fargs) - m the matrix - dimcode 1 applies the function to rows 2 applies the function to columns - f the function to be applied - fargs optional arguments to be supplied to f Programming in R

Avoiding loops Use apply over matrices and data frames Use lapply and sapply over lists Programming in R

Writing your own functions Function writing must always end with writing the value which should be returned You may also use ”return(value)” to show what value the function should return Programming in R

Writing your own functions If several arguments need to be returned, list may be used Programming in R

Writing your own functions Obligatory arguments and arguments by default Variables can be specified in any order (provided you specify the name) when you call the function Programming in R

Simulation The key to simulation is random number generation (RNG). R has several RNG implemented, default is the “Mersenne-Twister“ RNG. The sample() function can be used for random sampling from vectors and matrices Programming in R

Simulation R has functions to generate variates from many distributions ex. rnorm(),rbinom(),rchisq(),rpois(),rgamma(),mvrnorm() Programming in R

Simulation The set.seed() function can be used to generate reproducible sequences of random numbers Programming in R