R Crash Course Hengl

88
R crash course T. Hengl & R.S. Bivand Overview Course programme Why R? Software installation Scripting in R Do’s and don’ts R code editors Working with spatial data Spatial classes Spatial methods R+SAGA R+FWTools Export to Google Earth Literature R crash course (for people who pick up new methods/tools quickly and have a high motivation to learn) T. Hengl 1 & R.S. Bivand 2 1 Instituut voor Biodiversiteit en Ecosysteem Dynamica Universiteit van Amsterdam 2 Department of Economics Norwegian School of Economics and Business administration June 19, 2010

description

ds

Transcript of R Crash Course Hengl

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    R crash course(for people who pick up new methods/tools quickly

    and have a high motivation to learn)

    T. Hengl1 & R.S. Bivand2

    1Instituut voor Biodiversiteit en Ecosysteem DynamicaUniversiteit van Amsterdam

    2Department of EconomicsNorwegian School of Economics and Business administration

    June 19, 2010

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Programme

    Installation and first (baby) steps (intro.R).

    Building, running and editing models in R.

    Working with spatial data.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Crash course

    A crash course is a compressed version of a training course forpeople that already have full agendas. It is really intended for

    people who pick up new methods/tools quickly and have a highmotivation to learn (PhD students?). This means no longquestions, no going back, no long discussion, no complains

    about the speed/programme of the course. . . and of course: nopromises that you will manage to learn R in such a short time.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    The course programme

    Basic concepts (self-study)

    R basic and add-on packages;

    R syntax; R objects and methods;

    R FAQs; getting help and the most important literature;

    Scripting in R

    data management; creating and debugging scripts(scripting editors: Tinn-R);

    automating analysis making functions and packages;

    publication quality outputs (using R+Sweave);

    Advanced topics

    GDAL and R; spatial classes and packages;

    calling external applications from R (SAGA GIS, FWTools,Python, Matlab;

    export of maps to Google Earth;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Getting the right motivation (inspiration)

    What is R, and why should you invest time to learn it?

    What can it do? (and what it cant do?)

    How does the R community works (what are its sharedprinciples)?

    Is R suited for spatio-temporal data analysis?

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Some important assumptions

    R is (increasingly) sexy.

    R is hell.

    You are all beginners (who need a gentle intro).

    You are non-statisticians / computer scientists.

    I learned R myself. If I can do it, you can do it too.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Some important assumptions

    R is (increasingly) sexy.

    R is hell.

    You are all beginners (who need a gentle intro).

    You are non-statisticians / computer scientists.

    I learned R myself. If I can do it, you can do it too.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Some important assumptions

    R is (increasingly) sexy.

    R is hell.

    You are all beginners (who need a gentle intro).

    You are non-statisticians / computer scientists.

    I learned R myself. If I can do it, you can do it too.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Some important assumptions

    R is (increasingly) sexy.

    R is hell.

    You are all beginners (who need a gentle intro).

    You are non-statisticians / computer scientists.

    I learned R myself. If I can do it, you can do it too.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Some important assumptions

    R is (increasingly) sexy.

    R is hell.

    You are all beginners (who need a gentle intro).

    You are non-statisticians / computer scientists.

    I learned R myself. If I can do it, you can do it too.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What is R?

    the open source implementation of the S language forstatistical computing created by created by Ross Ihakaand Robert Gentleman (now maintained by the RDevelopment Core Team);

    why R?: the name R was selected for two reasons: (1)precedence Ris a letter beforeS, and (2) coincidence both of the creators names start with a letter R;

    it is a computer language developed to simplify statisticalcomputing/programming;

    it offers many contributed packages, technical documents,demos and examples;

    widely recognized as one of the fastest growing and mostcomprehensive statistical computing tools;

    it is a movement;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What is R?

    the open source implementation of the S language forstatistical computing created by created by Ross Ihakaand Robert Gentleman (now maintained by the RDevelopment Core Team);

    why R?: the name R was selected for two reasons: (1)precedence Ris a letter beforeS, and (2) coincidence both of the creators names start with a letter R;

    it is a computer language developed to simplify statisticalcomputing/programming;

    it offers many contributed packages, technical documents,demos and examples;

    widely recognized as one of the fastest growing and mostcomprehensive statistical computing tools;

    it is a movement;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What is R?

    the open source implementation of the S language forstatistical computing created by created by Ross Ihakaand Robert Gentleman (now maintained by the RDevelopment Core Team);

    why R?: the name R was selected for two reasons: (1)precedence Ris a letter beforeS, and (2) coincidence both of the creators names start with a letter R;

    it is a computer language developed to simplify statisticalcomputing/programming;

    it offers many contributed packages, technical documents,demos and examples;

    widely recognized as one of the fastest growing and mostcomprehensive statistical computing tools;

    it is a movement;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What is R?

    the open source implementation of the S language forstatistical computing created by created by Ross Ihakaand Robert Gentleman (now maintained by the RDevelopment Core Team);

    why R?: the name R was selected for two reasons: (1)precedence Ris a letter beforeS, and (2) coincidence both of the creators names start with a letter R;

    it is a computer language developed to simplify statisticalcomputing/programming;

    it offers many contributed packages, technical documents,demos and examples;

    widely recognized as one of the fastest growing and mostcomprehensive statistical computing tools;

    it is a movement;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What is R?

    the open source implementation of the S language forstatistical computing created by created by Ross Ihakaand Robert Gentleman (now maintained by the RDevelopment Core Team);

    why R?: the name R was selected for two reasons: (1)precedence Ris a letter beforeS, and (2) coincidence both of the creators names start with a letter R;

    it is a computer language developed to simplify statisticalcomputing/programming;

    it offers many contributed packages, technical documents,demos and examples;

    widely recognized as one of the fastest growing and mostcomprehensive statistical computing tools;

    it is a movement;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What is R?

    the open source implementation of the S language forstatistical computing created by created by Ross Ihakaand Robert Gentleman (now maintained by the RDevelopment Core Team);

    why R?: the name R was selected for two reasons: (1)precedence Ris a letter beforeS, and (2) coincidence both of the creators names start with a letter R;

    it is a computer language developed to simplify statisticalcomputing/programming;

    it offers many contributed packages, technical documents,demos and examples;

    widely recognized as one of the fastest growing and mostcomprehensive statistical computing tools;

    it is a movement;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    R is sexy

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Quote

    R has really become the second language for peoplecoming out of grad school now, and theres anamazing amount of code being written for it.

    Max Kuhn

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What it is NOT

    its not a software with GUI and point-and-click control;

    there are practically no data analysis wizards;

    its not an interactive system;

    it comes with no warranties (but so does the commercialsoftware too!!);

    there is practically no formal support;

    however. . .

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What it is NOT

    its not a software with GUI and point-and-click control;

    there are practically no data analysis wizards;

    its not an interactive system;

    it comes with no warranties (but so does the commercialsoftware too!!);

    there is practically no formal support;

    however. . .

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What it is NOT

    its not a software with GUI and point-and-click control;

    there are practically no data analysis wizards;

    its not an interactive system;

    it comes with no warranties (but so does the commercialsoftware too!!);

    there is practically no formal support;

    however. . .

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What it is NOT

    its not a software with GUI and point-and-click control;

    there are practically no data analysis wizards;

    its not an interactive system;

    it comes with no warranties (but so does the commercialsoftware too!!);

    there is practically no formal support;

    however. . .

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What it is NOT

    its not a software with GUI and point-and-click control;

    there are practically no data analysis wizards;

    its not an interactive system;

    it comes with no warranties (but so does the commercialsoftware too!!);

    there is practically no formal support;

    however. . .

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    What it is NOT

    its not a software with GUI and point-and-click control;

    there are practically no data analysis wizards;

    its not an interactive system;

    it comes with no warranties (but so does the commercialsoftware too!!);

    there is practically no formal support;

    however. . .

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Why make scripts?

    Its easy to use: Because S (and its implementation R) isa well-developed, simple and effective programminglanguage which includes conditionals, loops, user-definedrecursive functions and input and output facilities, existingfunctions can be modified. In R we all becomeprogrammers (but much faster than with C++ or Java).

    The basic approach to using R is to generate scripts thatdefine the data processing steps (workflows?).

    Documenting the analysis process is a good thing, soprogramming scripts are not just a burden, certainly forusers doing original research and repetitive work, arguablyfor student classes too.

    Point-and-click operations are for amateurs.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Why make scripts?

    Its easy to use: Because S (and its implementation R) isa well-developed, simple and effective programminglanguage which includes conditionals, loops, user-definedrecursive functions and input and output facilities, existingfunctions can be modified. In R we all becomeprogrammers (but much faster than with C++ or Java).

    The basic approach to using R is to generate scripts thatdefine the data processing steps (workflows?).

    Documenting the analysis process is a good thing, soprogramming scripts are not just a burden, certainly forusers doing original research and repetitive work, arguablyfor student classes too.

    Point-and-click operations are for amateurs.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Why make scripts?

    Its easy to use: Because S (and its implementation R) isa well-developed, simple and effective programminglanguage which includes conditionals, loops, user-definedrecursive functions and input and output facilities, existingfunctions can be modified. In R we all becomeprogrammers (but much faster than with C++ or Java).

    The basic approach to using R is to generate scripts thatdefine the data processing steps (workflows?).

    Documenting the analysis process is a good thing, soprogramming scripts are not just a burden, certainly forusers doing original research and repetitive work, arguablyfor student classes too.

    Point-and-click operations are for amateurs.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Why make scripts?

    Its easy to use: Because S (and its implementation R) isa well-developed, simple and effective programminglanguage which includes conditionals, loops, user-definedrecursive functions and input and output facilities, existingfunctions can be modified. In R we all becomeprogrammers (but much faster than with C++ or Java).

    The basic approach to using R is to generate scripts thatdefine the data processing steps (workflows?).

    Documenting the analysis process is a good thing, soprogramming scripts are not just a burden, certainly forusers doing original research and repetitive work, arguablyfor student classes too.

    Point-and-click operations are for amateurs.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Do you speak R?

    After some time you basically discovered that most of the thingsyou want to do, you can do in R, the only question is how.

    Well, first, you have to learn to how to speaknwrite R.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Why NOT Matlab?

    R and Matlab are in fact very similar, except. . . :

    (almost) everything you do in Matlab, you can do in R;

    R can be used to write homework (for Matlab you need acomputer lab with software licences);

    R gradually pushes people towards better programminghabits (Tamas K. Papp);

    Matlab has a limited support for processing spatial data (Ris better);

    Contributed packages in R are growing faster (R is better);

    We are in academia we shouldnt be developingtoolboxes for some private company but for the people(and for ourselves);

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    A bit of history

    R was first time released in 1997;

    majority of the development is (still) done by prof. BrianD. Ripley;

    a the moment (juni 19 2010 10:01), there are 2387contributed packages!

    according to Google trends, R-project.org has acommunity of about 200350k active users;

    in 2003, a group of researchers (International Workshop onDistributed Statistical Computing) decided to add Spatialfunctionality to R;

    . . . now is time to use it more broadly (MSc level and PhDlevel modules, projects, reports and scientific documents);

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Quote

    Once methodological problems start being perceivedor even defined in terms of what ones favoritesoftware does well, then the software has stoppedbeing a tool, and has become a crutch, and at worse ashackle.

    Simon Jackman

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    The ASDA(R)-book.org

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    The ASDA(R) team

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    A Practical Guide to Geostatistical Mapping

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Quote

    I created Quick-R for one simple reason. I wanted tolearn R and I am a teacher at heart. The easiest wayfor me to learn something is to teach it

    Robert I. Kabacoff

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Software

    Not all software is required to follow the exercises

    R v2.11 (Windows OS) including a list of packages;

    Tinn-R v2.3 (code editor);

    Optional: FWTools v2.4 a list of utilities to handlespatial data; SAGA GIS v2.0.4 a light GIS excellent foreducational purposes.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Installing the add-on packages

    > install.packages("ctv")

    > library(ctv)

    > install.views("Spatial")

    This will install all connected packages listed at R views Spatial.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Check your installation

    > Sys.getenv(c("OS", "COMPUTERNAME", "R_HOME", "R_LIBS_USER",

    + "PROCESSOR_IDENTIFIER"))

    OS

    "Windows_NT"

    COMPUTERNAME

    "PC-IBED193"

    R_HOME

    "C:\\PROGRA~1\\R\\R-210~1.1"

    R_LIBS_USER

    "n:/R/win-library/2.10"

    PROCESSOR_IDENTIFIER

    "x86 Family 6 Model 15 Stepping 6, GenuineIntel"

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Making friends with R

    1 Use script editors such as as TINN-R or JGR (syntaxhighlighting, wrapping, search/replace etc.).

    2 The best way to learn R is to look at the existing scripts,then extend:

    Graphical Manual/Addicted to R if you prefer to exploreexamples graphically;Teach-your-self books e.g. Statistics with R or AnIntroduction to R; Introduction to R for ITC studentsQuick-R and/or R by example;

    3 If your R script does not work, do not break your head, tryto get help:

    local installation: > help.search("kriging");R-project: > RSiteSearch("krige dimensions do notmatch")www: Rseek.orgR mailing lists;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Good practice

    Put comments in script (after #).

    Add some meta-information at the beginning of yourscript.

    Once you tested your script and saw that it works, tidy-upthe code.

    Place the input data on-line (database) so you cancommunicate only the script.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    R mailing lists: Dos and Donts!

    Dos:

    If you have not done so already, read the R posting guide.Use the existing pre-installed data sets (come together witha certain package) to describe your problem; link yourproblem to some existing problems.Acknowledge the work (time spent) other people do to helpyou.You can submit not only the problems you discover but alsothe information that you think is interesting for thecommunity.

    Donts:

    Do not send poorly formulated questions.Do not send too much.Complaining to a mailing list about what frustrates youabout R makes no sense.You are requesting that somebody does a work for you thatyou could do yourself!

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Tinn-R

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    JaGuaR

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Spatial objects

    An advantage of R (as compared to e.g. Matlab) is that youcan create your own formats and structures for data. But ifthere are too many formats you can easily get lots. In addition,we want to have smooth links to external formats (R is open!).

    To reduce this problem, Bivand et al. (2008) developednew-style classes to represent spatial data.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Spatial class

    The foundation object is the Spatial class, with just two basicslots (new-style S classes have pre-defined components calledslots):

    a bounding box mostly used for setting up plots;

    a CRS class object defining the coordinate referencesystem, and may be set to CRS(as.character(NA));

    Operations on Spatial* objects should update or copy thesevalues to the new Spatial* objects being created. The mostbasic spatial data object is a point, which may have 2 or 3dimensions.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Spatial classes

    for point features: SpatialPoints;SpatialPointsDataFrame;

    for line features: SpatialLines,SpatialLinesDataFrame;

    polygons: SpatialPolygons,SpatialPolygonsDataFrame;

    rasters: SpatialPixels, SpatialPixelsDataFrame,SpatialGrid, SpatialGridDataFrame;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    SpatialPoints

    > library(sp)

    > data(meuse)

    > coords summary(coords)

    Object of class SpatialPoints

    Coordinates:

    min max

    x 178605 181390

    y 329714 333611

    Is projected: NA

    proj4string : [NA]

    Number of points: 155

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    SpatialPointsDataFrame

    We can add the tabular data to make aSpatialPointsDataFrame object:

    > meuse1 str(meuse1, max.level = 2)

    Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots

    ..@ data :'data.frame': 155 obs. of 14 variables:

    ..@ coords.nrs : num(0)

    ..@ coords : num [1:155, 1:2] 181072 181025 181165 ...

    .. ..- attr(*, "dimnames")=List of 2

    ..@ bbox : num [1:2, 1:2] 178605 329714 181390 333611

    .. ..- attr(*, "dimnames")=List of 2

    ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Point features

    Spatial points

    Using the Meuse bank data set of soil samples and measurementsof heavy metal pollution provided with sp, well make aSpatialPoints object.> library(sp)

    > data(meuse)

    > coords summary(coords)

    Object of class SpatialPoints

    Coordinates:

    min max

    x 178605 181390

    y 329714 333611

    Is projected: NA

    proj4string : [NA]

    Number of points: 155

    Spatial points

    Now well add the original data frame to make aSpatialPointsDataFrame object. Many methods for standard dataframes just work with SpatialPointsDataFrame objects.> meuse1 names(meuse1)

    [1] "x" "y" "cadmium" "copper" "lead" "zinc"

    [7] "elev" "dist" "om" "ffreq" "soil" "lime"

    [13] "landuse" "dist.m"

    > summary(meuse1$zinc)

    Min. 1st Qu. Median Mean 3rd Qu. Max.

    113.0 198.0 326.0 469.7 674.5 1839.0

    > stem(meuse1$zinc, scale = 1/2)

    The decimal point is 2 digit(s) to the right of the |

    0 | 12223333344444455666677778888899999999

    2 | 000000011111112222233444555666678880022334455788

    4 | 00012235677001455556789

    6 | 01144678890012455678889

    8 | 0133113

    10 | 235604469

    12 | 8

    14 | 5357

    16 | 7

    18 | 4

    Spatial points classes and their slots

    coordsSpatial

    coords.nrsdata

    SpatialPoints bboxproj4string

    SpatialPointsDataFrame

    data.frame

    Spatial

    SpatialPoints

    Spatial lines and polygons

    I A Line object is just a spaghetti collection of 2D coordinates;a Polygon object is a Line object with equal first and lastcoordinates

    I A Lines object is a list of Line objects, such as all thecontours at a single elevation; the same relationship holdsbetween a Polygons object and a list of Polygon objects, suchas islands belonging to the same county

    I SpatialLines and SpatialPolygons objects are made usinglists of Lines or Polygons objects respectively

    I SpatialLinesDataFrame and SpatialPolygonsDataFrameobjects are defined using SpatialLines and SpatialPolygonsobjects and standard data frames, and the ID fields are hererequired to match the data frame row names

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Line/polygon featuresSpatial polygons

    The Meuse bank data set also includes the coordinates of the edgeof the river, linked together at the edge of the study area to form apolygon. We can make these coordinates into a SpatialPolygonsobject:> data(meuse.riv)

    > str(meuse.riv)

    num [1:176, 1:2] 182004 182137 182252 182314 182332 ...

    > river_polygon rivers summary(rivers)

    Object of class SpatialPolygons

    Coordinates:

    min max

    r1 178304.0 182331.5

    r2 325698.5 337684.8

    Is projected: NA

    proj4string : [NA]

    Spatial linesThere is a helper function contourLines2SLDF to convert the list ofcontours returned by contourLines into a SpatialLinesDataFrameobject. This example shows how the data slot row names matchthe ID slot values of the set of Lines objects making up theSpatialLinesDataFrame, note that some Lines objects includemultiple Line objects:> library(maptools)

    > volcano_sl row.names(slot(volcano_sl, "data"))

    [1] "C_1" "C_2" "C_3" "C_4" "C_5" "C_6" "C_7" "C_8" "C_9"

    [10] "C_10"

    > sapply(slot(volcano_sl, "lines"), function(x) slot(x,

    + "ID"))

    [1] "C_1" "C_2" "C_3" "C_4" "C_5" "C_6" "C_7" "C_8" "C_9"

    [10] "C_10"

    > sapply(slot(volcano_sl, "lines"), function(x) length(slot(x,

    + "Lines")))

    [1] 3 4 1 1 1 2 2 3 2 1

    > volcano_sl$level

    [1] 100 110 120 130 140 150 160 170 180 190

    Levels: 100 110 120 130 140 150 160 170 180 190

    Spatial Polygons classes and slots

    coordsSpatiallines

    plotOrderSpatial

    polygons

    bboxproj4string

    Polygon

    coords

    labptarea

    holeringDir

    LineLines

    ID

    Polygons

    plotOrderlabptIDarea

    SpatialLines

    Spatial

    SpatialPolygons

    Lines

    Polygons

    Spatial grids and pixels

    I There are two representations for data on regular rectangulargrids (oriented N-S, E-W): SpatialPixels and SpatialGrid

    I SpatialPixels are like SpatialPoints objects, but thecoordinates have to be regularly spaced; the coordinates arestored, as are grid indices

    I SpatialPixelsDataFrame objects only store attribute datawhere it is present, but need to store the coordinates and gridindices of those grid cells

    I SpatialGridDataFrame objects do not need to storecoordinates, because they fill the entire defined grid, but theyneed to store NA values where attribute values are missing

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Grids

    Spatial pixels

    Lets make a SpatialPixelsDataFrame object for the Meuse bankgrid data provided, with regular points at a 40m spacing. The datainclude soil types, flood frequency classes and distance from theriver bank:> data(meuse.grid)

    > coords meuseg1 names(meuseg1)

    [1] "x" "y" "part.a" "part.b" "dist" "soil" "ffreq"

    > slot(meuseg1, "grid")

    x y

    cellcentre.offset 178460 329620

    cellsize 40 40

    cells.dim 78 104

    > object.size(meuseg1)

    [1] 339036

    > dim(slot(meuseg1, "data"))

    [1] 3103 7

    Spatial grids

    In this case we convert the SpatialPixelsDataFrame object to aSpatialGridDataFrame by making a change in-place. In othercontexts, it is much more usual to create the GridTopology objectin the grid slot directly, and populate the grid from there, as wellsee later:> meuseg2 fullgrid(meuseg2) slot(meuseg2, "grid")

    x y

    cellcentre.offset 178460 329620

    cellsize 40 40

    cells.dim 78 104

    > class(slot(meuseg2, "grid"))

    [1] "GridTopology"

    attr(,"package")

    [1] "sp"

    > object.size(meuseg2)

    [1] 425684

    > dim(slot(meuseg2, "data"))

    [1] 8112 7

    Spatial grid and pixels classes and their slots

    dataSpatialPixels

    SpatialGriddata grid

    grid.indexSpatialPoints

    gridgrid.indexSpatialPoints

    cellcentre.offsetcellsizecells.dim

    coordsSpatial

    bboxproj4string

    data.frame

    Spatial

    GridTopology

    SpatialPoints

    SpatialGridDataFrameSpatialGrid

    SpatialPixelsSpatialPixelsDataFrame

    Spatial classes provided by sp

    This table summarises the classes provided by sp, and shows howthey build up to the objects of most practical use, theSpatial*DataFrame family objects:

    data type class attributes extendspoints SpatialPoints none Spatialpoints SpatialPointsDataFrame data.frame SpatialPointspixels SpatialPixels none SpatialPointspixels SpatialPixelsDataFrame data.frame SpatialPixels

    SpatialPointsDataFrame

    full grid SpatialGrid none SpatialPixelsfull grid SpatialGridDataFrame data.frame SpatialGridline Line nonelines Lines none Line listlines SpatialLines none Spatial, Lines listlines SpatialLinesDataFrame data.frame SpatialLinespolygon Polygon none Linepolygons Polygons none Polygon listpolygons SpatialPolygons none Spatial, Polygons listpolygons SpatialPolygonsDataFrame data.frame SpatialPolygons

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Basic methods

    spplot plotting of spatial objects (maps);

    spsample sample points from a set of polygons, on aset of lines or from a gridded area;

    bbox get the bounding box;

    proj4string get or set the projection (coordinatereference system);

    coordinates set or retrieve coordinates;

    spTransform transform coordinates from one CRS toanother;

    overlay combine two different spatial objects;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Plotting a SpatialPoints object

    > plot(as(meuse1, "Spatial"), axes = TRUE)

    > plot(meuse1, add = TRUE)

    > plot(meuse1[meuse1$ffreq == 1, ], col = "green", add = TRUE)

    178000 179000 180000 181000 182000

    3300

    0033

    1000

    3320

    0033

    3000

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Combining statistical and GIS operations

    Because the Spatial*DataFrame family objects behave inmost cases like data frames, most of what we are used todoing with standard data frames just works (but no merge,etc., yet).

    These objects are very similar to typical representations ofthe same kinds of objects in geographical informationsystems, so they do not suit spatial data that is notgeographical (like medical imaging) as such.

    Because now sp classes for GIS data exits, this opens thedoor for fusing GIS and statistical operations (this has notbeen possible in e.g. 2002).

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Spatial packages

    R now offers a range of contributed packages in spatialstatistics and increasing awareness of the importance of spatialdata analysis in the broader community. Current contributedpackages with spatial applications:

    point patterns: spatstat, VR:spatial, splancs;

    geostatistics: gstat, geoR, geoRglm, fields, spBayes,RandomFields, VR:spatial, sgeostat, vardiag;

    lattice/area data: spdep, DCluster, spgwr, ade4;

    links to GIS: rgdal, spgrass, RPy, RSAGA;

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Lets create spatial objects!

    We can create spatial objects from scratch! For example aDEM:

    > dem dem$Z gridded(dem) dem str(dem)

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    A small DEM

    > dem.plt print(dem.plt)

    > writeGDAL(dem, "dem6.sdat", "SAGA")

    DEM

    10

    20

    30

    40

    50

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Controlling SAGA from R

    > library(RSAGA)

    > rsaga.env()

    $workspace

    [1] "."

    $cmd

    [1] "saga_cmd.exe"

    $path

    [1] "C:/PROGRA~1/R/R-210~1.1/library/RSAGA/saga_vc"

    $modules

    [1] "C:/PROGRA~1/R/R-210~1.1/library/RSAGA/saga_vc/modules"

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Getting list of modules

    > rsaga.get.modules("ta_channels")

    $ta_channels

    code name interactive

    1 0 Channel Network FALSE

    2 1 Watershed Basins FALSE

    3 2 Watershed Basins (extended) FALSE

    4 3 Vertical Distance to Channel Network FALSE

    5 4 Overland Flow Distance to Channel Network FALSE

    6 5 D8 Flow Analysis FALSE

    7 6 Strahler Order FALSE

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Run stream extraction

    > rsaga.geoprocessor(lib = "ta_channels", module = 5, param = list(DEM = "dem6.sgrd",

    + DIRECTION = "channels.sgrd", CONNECTION = "route.sgrd",

    + NETWORK = "channels.shp"))

    SAGA CMD 2.0.4

    library path: C:/PROGRA~1/R/R-210~1.1/library/RSAGA/...

    library name: ta_channels

    module name : D8 Flow Analysis

    author : (c) 2003 by O.Conrad

    Load grid: dem6.sgrd...

    ready

    Parameters

    Grid system: 100; 6x 6y; 100x 100y

    DEM: dem6

    Flow Direction: Flow Direction

    Flow Connectivity: Flow Connectivity

    Flow Network: Flow Network

    Minimum Connectivity: 0

    ...

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Read back to R

    > dem$route channels

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Plot the final result

    > dem.plt channels.plt print(dem.plt, split = c(1, 1, 2, 1), more = T)

    > print(channels.plt, split = c(2, 1, 2, 1), more = F)

    DEM

    10

    20

    30

    40

    50

    Flow connectivity

    0.0

    0.5

    1.0

    1.5

    2.0

    2.5

    3.0

    3.5

    4.0

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Preparing FWTools

    There is still no package to control FWTools from R, but we cansimply send command lines using the system command. Beforewe can use FWTools from R, we need to locate it on our PC:

    > gdalwarp gdalwarp

    [1] "C:\\PROGRA~2\\FWTOOL~1.7\\bin\\gdalwarp.exe"

    > workd

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    MODIS data

    Now we can download some GIS data from web:

    > MOD12Q1 download.file(paste(MOD12Q1,

    + "MOD12Q1.A2004001.h18v03.004.2006117173748.hdf", sep=""),

    + destfile=paste(getwd(),

    + "MOD12Q1.A2004001.h18v03.004.2006117173748.hdf", sep="/"),

    + mode='wb', method='wget')

    Resolving e4ftl01u.ecs.nasa.gov... 152.61.4.83

    Connecting to e4ftl01u.ecs.nasa.gov|152.61.4.83|:21... connected.

    Logging in as anonymous ... Logged in!

    ==> SYST ... done. ==> PWD ... done.

    ==> TYPE I ... done. ==> CWD /MOLT/MOD12Q1.004/2004.01.01 ... done.

    ==> SIZE MOD12Q1.A2004001.h18v03.004.2006117173748.hdf ... 23165983

    ==> PASV ... done. ==> RETR MOD12Q1.A2004... done.

    Length: 23165983 (22M)

    0K .......... .......... 0% 64.9K 5m48s

    ...

    22550K .......... .......... 99% 501K 0s

    22600K .......... 100% 503K=65s

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Reprojecting grids

    We can reproject/resample the map to our local coordinatesystem using the gdalwarp functionality (this combines severalprocessing steps in one function):

    > NL.prj system(paste(gdalwarp, " HDF4_EOS:EOS_GRID:\"", workd,

    + "\\MOD12Q1.A2004001.h18v03.004.2006117173748.hdf\"

    + :MOD12Q1:Land_Cover_Type_1 -t_srs \"", NL.prj, "\"

    + IGBP2004NL.tif -r near -te 0 300000 280000 625000

    + -tr 500 500", sep=""))

    Creating output file that is 560P x 650L.

    Processing input file HDF4_EOS:EOS_GRID:\\MOD12Q1.A2004001...

    Using internal nodata values (eg. 255) for image HDF4_EOS:EOS_...

    0...10...20...30...40...50...60...70...80...90...100 - done.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Plot the final result

    In this case we have produced a MODIS-based land cover mapfor the whole Netherlands in resolution of 500 m (in localcoordinate system).

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Outline

    1 OverviewCourse programmeWhy R?Software installation

    2 Scripting in RDos and dontsR code editors

    3 Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    4 Literature

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Writing spatial data to KML

    There are two possibilities to export maps to KML: (a)using existing packages, and (b) by writing KML filesby-hand.

    To export point or line features to KML, use the writeOGRmethod that is available in R package rgdal.

    More flexible way to writing KML files is by using loops.

  • R crashcourse

    T. Hengl &R.S. Bivand

    Overview

    Course programme

    Why R?

    Software installation

    Scripting in R

    Dos and donts

    R code editors

    Working withspatial data

    Spatial classes

    Spatial methods

    R+SAGA

    R+FWTools

    Export to GoogleEarth

    Literature

    Literature

    Bivand, R., Pebesma, E., Rubio, V., 2008. Applied SpatialData Analysis with R. Use R Series, Springer, Heidelberg,378 p.

    Hengl, T., 2009. A Practical Guide to GeostatisticalMapping, 2nd edition. University of Amsterdam, 291 p.ISBN 978-90-9024981-0.

    Kabacoff, R.I., 2009. Data Analysis and Graphics with R.Manning publications, 375 p.

    Zuur, A. F., Ieno, E. N., Meesters, E. H. W. G., 2009. ABeginners Guide to R. Springer, Use R series, 228 p.

    OverviewCourse programmeWhy R?Software installation

    Scripting in RDo's and don'tsR code editors

    Working with spatial dataSpatial classesSpatial methodsR+SAGAR+FWToolsExport to Google Earth

    Literature