SlideShare a Scribd company logo
Essentials of a
Bas Kempen
© ISRIC-World Soil Information, 2017. Reproduction or dissemination of the work as a whole or parts is not permitted without
consent of the author. Sale or placement on a website where payment must be made to access the document is strictly prohibited. https://creativecommons.org/licenses/by-nc-nd/4.0/
What is R?
• R is a free software environment for statistical computing and
graphics
• R provides a wide variety of statistical (linear and nonlinear
modelling, classical statistical tests, time-series analysis,
classification, clustering, …) and graphical techniques, and is highly
extensible.
– Base functionality (comes with R installation)
– Extension via ‘Packages’ (~6,700)
• www.r-project.org/
• www.r-tutor.com
Why R?
• It is free
• It runs on a variety of platforms
• Platform for (advanced) statistical data analyses
• State-of-the-art graphic capabilities
• Connects with other software (SAGA GIS, GE, Python)
• Very large user community on the web; lots of resources
• R has a steep learning curve
• Thousands of packages, not always easy to find what you are
looking for
• Sometimes cryptic error messages
• Not a GIS
Working with R
RStudio
First steps.....
• Tell R where to find and save your files: setting the
working directory using the ‘setwd’ command:
– setwd(“D:/Bas/SpringSchool/Rintro/workingdir”)
– setwd(“D:BasSpringSchoolRintroworkingdir”)
• Load packages that are required for your analyses:
– library(gstat)
– require(gstat)
(packages should be installed on your computer)
• Note: R is case-sensitive!
Setting up your session
R scripts and data objects
• R scripts are saved as “<name>.R” files
• R objects in the environment can be saved for future
use.
• Save the entire environment or only a couple of
objects.
• Objects are saved as “<name>.rda” or
“<name>.RDATA” files
Basic Data Types
• Numeric
• Integer
• Logical
• Character
• Factor
Vector
Function
Vectors
• Sequence of data elements of the same basic
type.
• Vectors can be combined.
Vector arithmetic
• Vectorized operations: most operations work on vectors with
the same syntax as they work on scalars (no need for looping)
• Vector arithmetic:
• Recycling of vector elements:
Other data structures
• Matrices
• Lists
• Data frames
• Data frame is the fundamental data structure
for statistical modelling in R.
• Data frame is a table with columns and rows
(fields and records).
Data frame
• Columns can have different data types
(numeric, integer, logical, character, factor)
• All columns must have the same length
Selecting subsets
• Selection is done with ‘[...]’
• Vector:
• Data frame:
Functions
• Data analyses and modelling is done through functions.
• These can be very simple:
• More complex functions have multiple arguments (inputs)
• Arguments have specific requirements
• Access help: ?fit.variogram
Plotting
• Large number of packages and functions for
generating plots with basic functionality to
‘high-level’: e.g. lattice and ggplot.
• The basic function for plotting is ‘plot’
ggplot (http://ggplot2.org/)
• library(ggplot2)
• Build your plot layer by layer
• Building blocks:
– geom: the geometric object that describes the
type of plot that is produced.
– aes: ‘aesthetics’, defines the visual properties of
the variables that are going to be plotted.
– scales: control the legend, plot layout
Plotting with ggplot
Plotting with ggplot
Plotting with ggplot
Plotting with ggplot
Importing data
• Importing from tables:
– csv: read.csv()
– txt: read.table()
– xlxs: read.xlsx() [requires package ‘xlsx’]
• Data is imported as a data.frame
Exporting data
• Variety of exporting formats for tabular data:
• Saving plots
Working with spatial data in R
Spatial Data in R
• R offers a wide variety of packages and tools that
can handle spatial data.
• Note: R is not a GIS.
• R is not so memory efficient.
• Relevant packages:
– sp: handling spatial data
– raster: reading/manipulating/writing spatial raster
data
– rgdal: reading/writing spatial data
– maptools: reading/manipulating/writing spatial
polygon data (not maintained anymore)
Spatial data classes and formats
• Vector: points, lines and polygons (areal).
• For storing data that has discrete boundaries, such as
country borders, land parcels, and streets.
• Format: shapefile
Spatial data classes and formats
• Raster: surface divided into a regular grid of cells.
• For storing data that varies continuously, as in a
satellite image, a surface of chemical concentrations,
or an elevation surface.
• Format: GeoTiff (allows embedding spatial reference
information, metadata and color legends. It also
supports internal compression)
• (Ascii, ESRI Grid)
Structures for spatial data
• Spatial data is nothing more than a data frame
that has columns with X and Y coordinates.
• Example:
• Let’s now take a look at R classes for spatial data
(sp package)
Spatial data classes I
• Convert a data frame to a SpatialPointsDataFrame
object with the coordinates function.
Spatial data classes I
• Data frame with data points at regular
intervals.
Spatial data classes II
• Create a grid object with the gridded function:
SpatialPixelsDataFrame
Spatial data classes III
• Create a full grid object with the fullgrid function:
SpatialGridDataFrame
Spatial data classes IV
• The sp class for polygon data is the
SpatialPolygonsDataFrame.
Spatial data classes VI
• The raster package comes with its own class
for raster data: RasterLayer.
Importing spatial data
• rgdal: readOGR (vector), readGDAL (raster)
• maptools: readShapePoly, readShapePoints,
readShapeLines (functions not maintained anymore)
• raster: raster
Projections
• Once you have loaded your spatial data in R,
you have to tell R its geographic projection.
• Check the current projection: proj4string
function.
• Setting a projection: CRS function.
• Reprojecting to another coordinate system:
spTransform function.
Projections
Projections
Plotting
• sp package: spplot
Plotting
ggplot I
ggplot II
ggplot III
Interactive maps
• Interactive maps can be generated with the
leaflet package.
Exporting spatial data
• rgdal: writeOGR (vector), writeGDAL (raster)
• maptools: writePolyShape, writePointsShape
• raster: writeRaster
Now lets practice. Have fun!!

More Related Content

What's hot

All python data_analyst_r_course
All python data_analyst_r_courseAll python data_analyst_r_course
All python data_analyst_r_course
Kamal A
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics Platform
Syracuse University
 
Introduction to R Language
Introduction to R LanguageIntroduction to R Language
Introduction to R Language
Visuality
 
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake BolewskiThe TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
PyData
 
Working with Scientific Data in MATLAB
Working with Scientific Data in MATLABWorking with Scientific Data in MATLAB
Working with Scientific Data in MATLAB
The HDF-EOS Tools and Information Center
 
TileDB
TileDBTileDB
R data structures-2
R data structures-2R data structures-2
R data structures-2
Victor Ordu
 
SPD and KEA: HDF5 based file formats for Earth Observation
SPD and KEA: HDF5 based file formats for Earth ObservationSPD and KEA: HDF5 based file formats for Earth Observation
SPD and KEA: HDF5 based file formats for Earth Observation
The HDF-EOS Tools and Information Center
 
Apache Spark — Fundamentals and MLlib
Apache Spark — Fundamentals and MLlibApache Spark — Fundamentals and MLlib
Apache Spark — Fundamentals and MLlib
Jens Fisseler, Dr.
 
R programming language
R programming languageR programming language
R programming language
Keerti Verma
 
Lec_5_Intro to Attributes
Lec_5_Intro to AttributesLec_5_Intro to Attributes
Lec_5_Intro to Attributes
Atiqa khan
 
Pilot Project for HDF5 Metadata Structures for SWOT
Pilot Project for HDF5 Metadata Structures for SWOTPilot Project for HDF5 Metadata Structures for SWOT
Pilot Project for HDF5 Metadata Structures for SWOT
The HDF-EOS Tools and Information Center
 
A seminar on neo4 j
A seminar on neo4 jA seminar on neo4 j
A seminar on neo4 j
Rishikese MR
 
A brief introduction to 'R' statistical package
A brief introduction to 'R' statistical packageA brief introduction to 'R' statistical package
A brief introduction to 'R' statistical package
Shanmukha S. Potti
 
2020 Vision (Dubious Design Decisions)
2020 Vision (Dubious Design Decisions)2020 Vision (Dubious Design Decisions)
2020 Vision (Dubious Design Decisions)
Alex Henderson
 
DE Presentation v2
DE Presentation v2DE Presentation v2
DE Presentation v2
scstark
 
LDBC 8th TUC Meeting: Introduction and status update
LDBC 8th TUC Meeting: Introduction and status updateLDBC 8th TUC Meeting: Introduction and status update
LDBC 8th TUC Meeting: Introduction and status update
LDBC council
 
Working with HDF and netCDF Data in ArcGIS: Tools and Case Studies
Working with HDF and netCDF Data in ArcGIS: Tools and Case StudiesWorking with HDF and netCDF Data in ArcGIS: Tools and Case Studies
Working with HDF and netCDF Data in ArcGIS: Tools and Case Studies
The HDF-EOS Tools and Information Center
 

What's hot (20)

All python data_analyst_r_course
All python data_analyst_r_courseAll python data_analyst_r_course
All python data_analyst_r_course
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics Platform
 
Introduction to R Language
Introduction to R LanguageIntroduction to R Language
Introduction to R Language
 
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake BolewskiThe TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
The TileDB Array Data Storage Manager - Stavros Papadopoulos, Jake Bolewski
 
Pandas
PandasPandas
Pandas
 
Working with Scientific Data in MATLAB
Working with Scientific Data in MATLABWorking with Scientific Data in MATLAB
Working with Scientific Data in MATLAB
 
TileDB
TileDBTileDB
TileDB
 
R data structures-2
R data structures-2R data structures-2
R data structures-2
 
SPD and KEA: HDF5 based file formats for Earth Observation
SPD and KEA: HDF5 based file formats for Earth ObservationSPD and KEA: HDF5 based file formats for Earth Observation
SPD and KEA: HDF5 based file formats for Earth Observation
 
Apache Spark — Fundamentals and MLlib
Apache Spark — Fundamentals and MLlibApache Spark — Fundamentals and MLlib
Apache Spark — Fundamentals and MLlib
 
R programming language
R programming languageR programming language
R programming language
 
Lec_5_Intro to Attributes
Lec_5_Intro to AttributesLec_5_Intro to Attributes
Lec_5_Intro to Attributes
 
Pilot Project for HDF5 Metadata Structures for SWOT
Pilot Project for HDF5 Metadata Structures for SWOTPilot Project for HDF5 Metadata Structures for SWOT
Pilot Project for HDF5 Metadata Structures for SWOT
 
A seminar on neo4 j
A seminar on neo4 jA seminar on neo4 j
A seminar on neo4 j
 
A brief introduction to 'R' statistical package
A brief introduction to 'R' statistical packageA brief introduction to 'R' statistical package
A brief introduction to 'R' statistical package
 
Hadoop intro
Hadoop introHadoop intro
Hadoop intro
 
2020 Vision (Dubious Design Decisions)
2020 Vision (Dubious Design Decisions)2020 Vision (Dubious Design Decisions)
2020 Vision (Dubious Design Decisions)
 
DE Presentation v2
DE Presentation v2DE Presentation v2
DE Presentation v2
 
LDBC 8th TUC Meeting: Introduction and status update
LDBC 8th TUC Meeting: Introduction and status updateLDBC 8th TUC Meeting: Introduction and status update
LDBC 8th TUC Meeting: Introduction and status update
 
Working with HDF and netCDF Data in ArcGIS: Tools and Case Studies
Working with HDF and netCDF Data in ArcGIS: Tools and Case StudiesWorking with HDF and netCDF Data in ArcGIS: Tools and Case Studies
Working with HDF and netCDF Data in ArcGIS: Tools and Case Studies
 

Similar to Essentials of R

Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL Server
Stéphane Fréchette
 
Arc gis introduction-ppt
Arc gis introduction-pptArc gis introduction-ppt
Arc gis introduction-pptAshok Peddi
 
Apdm 101 Arc Gis Pipeline Data Model (1)
Apdm 101 Arc Gis Pipeline Data Model  (1)Apdm 101 Arc Gis Pipeline Data Model  (1)
Apdm 101 Arc Gis Pipeline Data Model (1)
David Nichter, GISP
 
Big data analytics with R tool.pptx
Big data analytics with R tool.pptxBig data analytics with R tool.pptx
Big data analytics with R tool.pptx
salutiontechnology
 
Materi Geodatabase Management - Fellowship 2022.pdf
Materi Geodatabase Management - Fellowship 2022.pdfMateri Geodatabase Management - Fellowship 2022.pdf
Materi Geodatabase Management - Fellowship 2022.pdf
sakinatunnajmi
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.ppt
Anshika865276
 
How to obtain and install R.ppt
How to obtain and install R.pptHow to obtain and install R.ppt
How to obtain and install R.ppt
rajalakshmi5921
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
vikassingh569137
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MD
SonaCharles2
 
17641.ppt
17641.ppt17641.ppt
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICS
HaritikaChhatwal1
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
Dr. Awase Khirni Syed
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data science
Long Nguyen
 
Analytics Beyond RAM Capacity using R
Analytics Beyond RAM Capacity using RAnalytics Beyond RAM Capacity using R
Analytics Beyond RAM Capacity using R
Alex Palamides
 
Geodatabase design steps for students.pptx
Geodatabase design steps for students.pptxGeodatabase design steps for students.pptx
Geodatabase design steps for students.pptx
azadimran555
 
Week-3 – System RSupplemental material1Recap •.docx
Week-3 – System RSupplemental material1Recap •.docxWeek-3 – System RSupplemental material1Recap •.docx
Week-3 – System RSupplemental material1Recap •.docx
helzerpatrina
 
Step By Step Guide to Learn R
Step By Step Guide to Learn RStep By Step Guide to Learn R
Step By Step Guide to Learn R
Venkata Reddy Konasani
 
Using R to Visualize Spatial Data: R as GIS - Guy Lansley
Using R to Visualize Spatial Data: R as GIS - Guy LansleyUsing R to Visualize Spatial Data: R as GIS - Guy Lansley
Using R to Visualize Spatial Data: R as GIS - Guy Lansley
Guy Lansley
 
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
مركز البحوث الأقسام العلمية
 

Similar to Essentials of R (20)

Data Analytics with R and SQL Server
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL Server
 
Arc gis introduction-ppt
Arc gis introduction-pptArc gis introduction-ppt
Arc gis introduction-ppt
 
Apdm 101 Arc Gis Pipeline Data Model (1)
Apdm 101 Arc Gis Pipeline Data Model  (1)Apdm 101 Arc Gis Pipeline Data Model  (1)
Apdm 101 Arc Gis Pipeline Data Model (1)
 
Big data analytics with R tool.pptx
Big data analytics with R tool.pptxBig data analytics with R tool.pptx
Big data analytics with R tool.pptx
 
Materi Geodatabase Management - Fellowship 2022.pdf
Materi Geodatabase Management - Fellowship 2022.pdfMateri Geodatabase Management - Fellowship 2022.pdf
Materi Geodatabase Management - Fellowship 2022.pdf
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.ppt
 
design_doc
design_docdesign_doc
design_doc
 
How to obtain and install R.ppt
How to obtain and install R.pptHow to obtain and install R.ppt
How to obtain and install R.ppt
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MD
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICS
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data science
 
Analytics Beyond RAM Capacity using R
Analytics Beyond RAM Capacity using RAnalytics Beyond RAM Capacity using R
Analytics Beyond RAM Capacity using R
 
Geodatabase design steps for students.pptx
Geodatabase design steps for students.pptxGeodatabase design steps for students.pptx
Geodatabase design steps for students.pptx
 
Week-3 – System RSupplemental material1Recap •.docx
Week-3 – System RSupplemental material1Recap •.docxWeek-3 – System RSupplemental material1Recap •.docx
Week-3 – System RSupplemental material1Recap •.docx
 
Step By Step Guide to Learn R
Step By Step Guide to Learn RStep By Step Guide to Learn R
Step By Step Guide to Learn R
 
Using R to Visualize Spatial Data: R as GIS - Guy Lansley
Using R to Visualize Spatial Data: R as GIS - Guy LansleyUsing R to Visualize Spatial Data: R as GIS - Guy Lansley
Using R to Visualize Spatial Data: R as GIS - Guy Lansley
 
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
 

More from ExternalEvents

Mauritania
Mauritania Mauritania
Mauritania
ExternalEvents
 
Malawi - M. Munthali
Malawi - M. MunthaliMalawi - M. Munthali
Malawi - M. Munthali
ExternalEvents
 
Malawi (Mbewe)
Malawi (Mbewe)Malawi (Mbewe)
Malawi (Mbewe)
ExternalEvents
 
Malawi (Desideri)
Malawi (Desideri)Malawi (Desideri)
Malawi (Desideri)
ExternalEvents
 
Lesotho
LesothoLesotho
Kenya
KenyaKenya
ICRAF: Soil-plant spectral diagnostics laboratory
ICRAF: Soil-plant spectral diagnostics laboratoryICRAF: Soil-plant spectral diagnostics laboratory
ICRAF: Soil-plant spectral diagnostics laboratory
ExternalEvents
 
Ghana
GhanaGhana
Ethiopia
EthiopiaEthiopia
Ethiopia
ExternalEvents
 
Item 15
Item 15Item 15
Item 14
Item 14Item 14
Item 13
Item 13Item 13
Item 7
Item 7Item 7
Item 6
Item 6Item 6
Item 3
Item 3Item 3
Item 16
Item 16Item 16
Item 9: Soil mapping to support sustainable agriculture
Item 9: Soil mapping to support sustainable agricultureItem 9: Soil mapping to support sustainable agriculture
Item 9: Soil mapping to support sustainable agriculture
ExternalEvents
 
Item 8: WRB, World Reference Base for Soil Resouces
Item 8: WRB, World Reference Base for Soil ResoucesItem 8: WRB, World Reference Base for Soil Resouces
Item 8: WRB, World Reference Base for Soil Resouces
ExternalEvents
 
Item 7: Progress made in Nepal
Item 7: Progress made in NepalItem 7: Progress made in Nepal
Item 7: Progress made in Nepal
ExternalEvents
 
Item 6: International Center for Biosaline Agriculture
Item 6: International Center for Biosaline AgricultureItem 6: International Center for Biosaline Agriculture
Item 6: International Center for Biosaline Agriculture
ExternalEvents
 

More from ExternalEvents (20)

Mauritania
Mauritania Mauritania
Mauritania
 
Malawi - M. Munthali
Malawi - M. MunthaliMalawi - M. Munthali
Malawi - M. Munthali
 
Malawi (Mbewe)
Malawi (Mbewe)Malawi (Mbewe)
Malawi (Mbewe)
 
Malawi (Desideri)
Malawi (Desideri)Malawi (Desideri)
Malawi (Desideri)
 
Lesotho
LesothoLesotho
Lesotho
 
Kenya
KenyaKenya
Kenya
 
ICRAF: Soil-plant spectral diagnostics laboratory
ICRAF: Soil-plant spectral diagnostics laboratoryICRAF: Soil-plant spectral diagnostics laboratory
ICRAF: Soil-plant spectral diagnostics laboratory
 
Ghana
GhanaGhana
Ghana
 
Ethiopia
EthiopiaEthiopia
Ethiopia
 
Item 15
Item 15Item 15
Item 15
 
Item 14
Item 14Item 14
Item 14
 
Item 13
Item 13Item 13
Item 13
 
Item 7
Item 7Item 7
Item 7
 
Item 6
Item 6Item 6
Item 6
 
Item 3
Item 3Item 3
Item 3
 
Item 16
Item 16Item 16
Item 16
 
Item 9: Soil mapping to support sustainable agriculture
Item 9: Soil mapping to support sustainable agricultureItem 9: Soil mapping to support sustainable agriculture
Item 9: Soil mapping to support sustainable agriculture
 
Item 8: WRB, World Reference Base for Soil Resouces
Item 8: WRB, World Reference Base for Soil ResoucesItem 8: WRB, World Reference Base for Soil Resouces
Item 8: WRB, World Reference Base for Soil Resouces
 
Item 7: Progress made in Nepal
Item 7: Progress made in NepalItem 7: Progress made in Nepal
Item 7: Progress made in Nepal
 
Item 6: International Center for Biosaline Agriculture
Item 6: International Center for Biosaline AgricultureItem 6: International Center for Biosaline Agriculture
Item 6: International Center for Biosaline Agriculture
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 

Essentials of R

  • 1. Essentials of a Bas Kempen © ISRIC-World Soil Information, 2017. Reproduction or dissemination of the work as a whole or parts is not permitted without consent of the author. Sale or placement on a website where payment must be made to access the document is strictly prohibited. https://creativecommons.org/licenses/by-nc-nd/4.0/
  • 2. What is R? • R is a free software environment for statistical computing and graphics • R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible. – Base functionality (comes with R installation) – Extension via ‘Packages’ (~6,700) • www.r-project.org/ • www.r-tutor.com
  • 3. Why R? • It is free • It runs on a variety of platforms • Platform for (advanced) statistical data analyses • State-of-the-art graphic capabilities • Connects with other software (SAGA GIS, GE, Python) • Very large user community on the web; lots of resources • R has a steep learning curve • Thousands of packages, not always easy to find what you are looking for • Sometimes cryptic error messages • Not a GIS
  • 6. First steps..... • Tell R where to find and save your files: setting the working directory using the ‘setwd’ command: – setwd(“D:/Bas/SpringSchool/Rintro/workingdir”) – setwd(“D:BasSpringSchoolRintroworkingdir”) • Load packages that are required for your analyses: – library(gstat) – require(gstat) (packages should be installed on your computer) • Note: R is case-sensitive!
  • 7. Setting up your session
  • 8. R scripts and data objects • R scripts are saved as “<name>.R” files • R objects in the environment can be saved for future use. • Save the entire environment or only a couple of objects. • Objects are saved as “<name>.rda” or “<name>.RDATA” files
  • 9. Basic Data Types • Numeric • Integer • Logical • Character • Factor Vector Function
  • 10. Vectors • Sequence of data elements of the same basic type. • Vectors can be combined.
  • 11. Vector arithmetic • Vectorized operations: most operations work on vectors with the same syntax as they work on scalars (no need for looping) • Vector arithmetic: • Recycling of vector elements:
  • 12. Other data structures • Matrices • Lists • Data frames • Data frame is the fundamental data structure for statistical modelling in R. • Data frame is a table with columns and rows (fields and records).
  • 13. Data frame • Columns can have different data types (numeric, integer, logical, character, factor) • All columns must have the same length
  • 14. Selecting subsets • Selection is done with ‘[...]’ • Vector: • Data frame:
  • 15. Functions • Data analyses and modelling is done through functions. • These can be very simple: • More complex functions have multiple arguments (inputs) • Arguments have specific requirements • Access help: ?fit.variogram
  • 16. Plotting • Large number of packages and functions for generating plots with basic functionality to ‘high-level’: e.g. lattice and ggplot. • The basic function for plotting is ‘plot’
  • 17. ggplot (http://ggplot2.org/) • library(ggplot2) • Build your plot layer by layer • Building blocks: – geom: the geometric object that describes the type of plot that is produced. – aes: ‘aesthetics’, defines the visual properties of the variables that are going to be plotted. – scales: control the legend, plot layout
  • 22. Importing data • Importing from tables: – csv: read.csv() – txt: read.table() – xlxs: read.xlsx() [requires package ‘xlsx’] • Data is imported as a data.frame
  • 23. Exporting data • Variety of exporting formats for tabular data: • Saving plots
  • 24. Working with spatial data in R
  • 25. Spatial Data in R • R offers a wide variety of packages and tools that can handle spatial data. • Note: R is not a GIS. • R is not so memory efficient. • Relevant packages: – sp: handling spatial data – raster: reading/manipulating/writing spatial raster data – rgdal: reading/writing spatial data – maptools: reading/manipulating/writing spatial polygon data (not maintained anymore)
  • 26. Spatial data classes and formats • Vector: points, lines and polygons (areal). • For storing data that has discrete boundaries, such as country borders, land parcels, and streets. • Format: shapefile
  • 27. Spatial data classes and formats • Raster: surface divided into a regular grid of cells. • For storing data that varies continuously, as in a satellite image, a surface of chemical concentrations, or an elevation surface. • Format: GeoTiff (allows embedding spatial reference information, metadata and color legends. It also supports internal compression) • (Ascii, ESRI Grid)
  • 28. Structures for spatial data • Spatial data is nothing more than a data frame that has columns with X and Y coordinates. • Example: • Let’s now take a look at R classes for spatial data (sp package)
  • 29. Spatial data classes I • Convert a data frame to a SpatialPointsDataFrame object with the coordinates function.
  • 30. Spatial data classes I • Data frame with data points at regular intervals.
  • 31. Spatial data classes II • Create a grid object with the gridded function: SpatialPixelsDataFrame
  • 32. Spatial data classes III • Create a full grid object with the fullgrid function: SpatialGridDataFrame
  • 33. Spatial data classes IV • The sp class for polygon data is the SpatialPolygonsDataFrame.
  • 34. Spatial data classes VI • The raster package comes with its own class for raster data: RasterLayer.
  • 35. Importing spatial data • rgdal: readOGR (vector), readGDAL (raster) • maptools: readShapePoly, readShapePoints, readShapeLines (functions not maintained anymore) • raster: raster
  • 36. Projections • Once you have loaded your spatial data in R, you have to tell R its geographic projection. • Check the current projection: proj4string function. • Setting a projection: CRS function. • Reprojecting to another coordinate system: spTransform function.
  • 44. Interactive maps • Interactive maps can be generated with the leaflet package.
  • 45. Exporting spatial data • rgdal: writeOGR (vector), writeGDAL (raster) • maptools: writePolyShape, writePointsShape • raster: writeRaster
  • 46. Now lets practice. Have fun!!