R for Macroecology Aarhus University, Spring 2011.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Matlab
Introduction to R Brody Sandel. Topics Approaching your analysis Basic structure of R Basic programming Plotting Spatial data.
Chapter 7: Arrays In this chapter, you will learn about
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
An Introduction to R: Logic & Basics. The R language Command line Can be executed within a terminal Within Emacs using ESS (Emacs Speaks Statistics)
R for Macroecology Functions and plotting. A few words on for  for( i in 1:10 )
Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Arrays.
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
Matlab Tutorial Course
Introduction to MATLAB The language of Technical Computing.
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
Lecture 2 LISAM. Statistical software.. LISAM What is LISAM? Social network for Creating personal pages Creating courses  Storing course materials (lectures,
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
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.
Hands-on Introduction to R. Outline R : A powerful Platform for Statistical Analysis Why bother learning R ? Data, data, data, I cannot make bricks without.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Introduction to to R Emily Kalah Gade University of Washington Credit to Kristin Siebel for development of much of this PowerPoint.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Introduction to R Part 1. First Note: I am not an expert at R. – I’ve been hiking up the learning curve for about a year. You can learn R. – You will.
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.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
CSC Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Linux Operations and Administration
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.
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.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Introduction to R Introductions What is R? RStudio Layout Summary Statistics Your First R Graph 17 September 2014 Sherubtse Training.
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.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
STAT 534: Statistical Computing Hari Narayanan
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Data & Graphing vectors data frames importing data contingency tables barplots 18 September 2014 Sherubtse Training.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
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:
Introduction to R Chris Free. Introduction to R Free! Superior (if not comparable) to commercial alternatives Available on all platforms Not just for.
Review > x[-c(1,4,6)] > Y[1:3,2:8] > island.data fishData$weight[1] > fishData[fishData$weight < 20 & fishData$condition.
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) –
Programming in R Intro, data and programming structures
Introduction to Matlab
L – Modeling and Simulating Social Systems with MATLAB
Matlab Workshop 9/22/2018.
Introduction to MATLAB
ECONOMETRICS ii – spring 2018
Communication and Coding Theory Lab(CS491)
INTRODUCTION TO MATLAB
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.
R Course 1st Lecture.
Introduction to Matlab
Data analysis with R and the tidyverse
Matlab Basics Tutorial
Presentation transcript:

R for Macroecology Aarhus University, Spring 2011

Why use R  Scripted  Flexible  Free  Many extensions available  Huge support community

Brody’s rule of computers  Computers make hard things easy and easy things hard  The more sophisticated you get, the more true this becomes (e.g. Excel vs. R)  Be prepared to spend lots of time on stupid things, but know that the hard things will get done fast

The schedule  Introduction to R and programming  Functions and plotting  Model specification, tests, and selection  Spatial data in R, integration with GIS  Spatial structure in data  Simultaneous autoregressive models  Project introduction (1 week) and work (2 weeks)  Presentation of project results

Today  The structure of R  Functions, objects and programming  Reading and writing data

What is R?  R is a statistical programming language  Scripts  Plotting  System commands  The scripting interface in R is not very pretty  PC – Tinn-R  Apple – TextWrangler  Rstudio  All provide syntax highlighting (very useful!)

The structure of R  Functions  Objects  Control elements

The structure of R  Functions (what do you want to do?)  Objects (what do you want to do it to?)  Control elements (when/how often do you want to do it?)

The structure of R Object Function Object

The structure of R Object Function Object

The structure of R Object Function Object Options

The structure of R Object Function Object Options Arguments Return

Controlled by control elements (for, while, if) The structure of R Object Function Object Options

Calling a function  Call: a function with a particular set of arguments  function( argument, argument... )  x = function( argument, argument...) sqrt(16) [1] 4 x = sqrt(16) x [1] 4

Calling a function  Call: a function with a particular set of arguments  function( argument, argument... )  x = function( argument, argument...) sqrt(16) [1] 4 x = sqrt(16) x [1] 4 The function return is not saved, just printed to the screen

Calling a function  Call: a function with a particular set of arguments  function( argument, argument... )  x = function( argument, argument...) sqrt(16) [1] 4 x = sqrt(16) x [1] 4 The function return is saved to a new object, “x”

Arguments to a function  function( argument, argument...)  Many functions will have default values for arguments  If unspecified, the argument will take that value  To find these values and a list of all arguments, do:  If you are just looking for functions related to a word, I would use google. But you can also: ?function.name ??key.word

What is an object?  What size is it?  Vector (one-dimensional, including length = 1)  Matrix (two-dimensional)  Array (n-dimensional)  What does it hold?  Numeric (0, 0.2, Inf, NA)  Logical (T, F)  Factor (“Male”, “Female”)  Character (“Bromus diandrus”, “Bromus carinatus”, “Bison bison”)  Mixtures  Lists  Dataframes  class() is a function that tells you what type of object the argument is

Creating a numeric object a = 10 a [1] 10 a <- 10 a [1] > a a [1] 10

Creating a numeric object a = 10 a [1] 10 a <- 10 a [1] > a a [1] 10 All of these are assignments

Creating a numeric object a = a + 1 a [1] 11 b = a * a b [1] 121 x = sqrt(b) x [1] 11

Creating a numeric object (length >1) a = c(4,2,5,10) a [1] a = 1:4 a [1] a = seq(1,10) a [1]

a = c(4,2,5,10) a [1] a = 1:4 a [1] a = seq(1,10) a [1] Two arguments passed to this function! Creating a numeric object (length >1)

a = c(4,2,5,10) a [1] a = 1:4 a [1] a = seq(1,10) a [1] This function returns a vector Creating a numeric object (length >1)

Creating a matrix object A = matrix(data = 0, nrow = 6, ncol = 5) A [,1] [,2] [,3] [,4] [,5] [1,] [2,] [3,] [4,] [5,] [6,]

Creating a logical object 3 < 5 [1] TRUE 3 > 5 [1] FALSE x = 5 x == 5 [1] TRUE x != 5 [1] FALSE = == != %in% & | Conditional operators

Creating a logical object 3 < 5 [1] TRUE 3 > 5 [1] FALSE x = 5 x == 5 [1] TRUE x != 5 [1] FALSE Very important to remember this difference!!! = == != %in% & | Conditional operators

Creating a logical object x = 1:10 x < 5 [1] TRUE TRUE TRUE TRUE FALSE [6] FALSE FALSE FALSE FALSE FALSE x == 2 [1] FALSE TRUE FALSE FALSE FALSE [6] FALSE FALSE FALSE FALSE FALSE = == != %in% & | Conditional operators

Getting at values  R uses [ ] to refer to elements of objects  For example:  V[5] returns the 5 th element of a vector called V  M[2,3] returns the element in the 2 nd row, 3 rd column of matrix M  M[2,] returns all elements in the 2 nd row of matrix M  The number inside the brackets is called an index

Getting at a value from a numeric a = c(3,2,7,8) a[3] [1] 7 a[1:3] [1] a[seq(2,4)] [1] 2 7 8

Getting at a value from a numeric a = c(3,2,7,8) a[3] [1] 7 a[1:3] [1] a[seq(2,4)] [1] See what I did there?

Just for fun... a = c(3,2,7,8) a[a]

Just for fun... a = c(3,2,7,8) a[a] [1] 7 2 NA NA When would a[a] return a?

Getting at values - matrices A = matrix(data = 0, nrow = 6, ncol = 5) A [,1] [,2] [,3] [,4] [,5] [1,] [2,] [3,] [4,] [5,] [6,] A[3,4] [1] 0 The order is always [row, column]

Lists  A list is a generic holder of other variable types  Each element of a list can be anything (even another list!) a = c(1,2,3) b = c(10,20,30) L = list(a,b) L [[1]] [1] [[2]] [3] L[[1]] [1] L[[2]][2] [1] 20

A break to try things out  Practicing with the function seq()  Create vectors and matrices in a few different ways

Programming in R Functions Loop

Programming in R Functions if Functions if Output Loop

Next topic: control elements  for  if  while  The general syntax is: for/if/while ( conditions ) { commands }

For  When you want to do something a certain number of times  When you want to do something to each element of a vector, list, matrix... X = seq(1,4,by = 1) for(i in X) { print(i+1) } [1] 2 [1] 3 [1] 4 [1] 5

If  When you want to execute a bit of code only if some condition is true X = 25 if( X < 22 ) { print(X+1) } X = 20 if( X < 22 ) { print(X+1) } [1] 21 = == != %in% & |

If/else  Do one thing or the other X = 10 if( X < 22 ) { X+1 }else(sqrt(X)) [1] 11 X = 25 if( X < 22 ) { X+1 }else(sqrt(X)) [1] 5 = == != %in% & |

While  Do something as long as a condition is TRUE i = 1 while( i < 5 ) { i = i + 1 } i [1] 5 = == != %in% & |

Practice with these a bit  For loops  While loops

Next topic: working with data  Principles  Read data off of hard drive  R stores it as an object (saved in your computer’s memory)  Treat that object like any other  Changes to the object are restricted to the object, they don’t affect the data on the hard drive

Working directory  The directory where R looks for files, or writes files  setwd() changes it  dir() shows the contents of it setwd(“C:/Project Directory/”) dir() [1] “a figure.pdf” [2] “more data.csv” [3] “some data.csv”

Read a data file setwd(“C:/Project Directory/”) dir() [1] “a figure.pdf” [2] “more data.csv” [3] “some data.csv” myData = read.csv(“some data.csv”)

Writing a data file setwd(“C:/Project Directory/”) dir() [1] “a figure.pdf” [2] “more data.csv” [3] “some data.csv” myData = read.csv(“some data.csv”) write.csv(myData,”updated data.csv”) dir() [1] “a figure.pdf” [2] “more data.csv” [3] “some data.csv” [4] “updated data.csv”

Finding your way around a data frame  head() shows the first few lines  tail() shows the last few  names() gives the column names  Pulling out columns  Data$columnname  Data[,columnname]  Data[,3] (if columnname is the 3 rd column)

Doing things to data frames  apply!  On the board – compare for loop to apply

Practice with these  Homework – I do not care about the answers to questions, I care about the scripts you used to get them  Save your scripts!  Turn them in to me next week  Talk to me during the week if you have any trouble