0% found this document useful (0 votes)
50 views2 pages

R Cheat Sheet For The Ethiosis Crash Course On DSM: Organizing Data

This document provides a brief summary of useful R functions for the EthioSIS crash course on digital soil mapping. It outlines functions for organizing data, loading packages and data, conducting statistical analysis and math, variography and kriging, plotting, and linear regression. The document emphasizes that data are stored as spatial objects from the sp package and recommends functions from base R, sp, gstat, and ggplot2 packages.

Uploaded by

Geta Tiruneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views2 pages

R Cheat Sheet For The Ethiosis Crash Course On DSM: Organizing Data

This document provides a brief summary of useful R functions for the EthioSIS crash course on digital soil mapping. It outlines functions for organizing data, loading packages and data, conducting statistical analysis and math, variography and kriging, plotting, and linear regression. The document emphasizes that data are stored as spatial objects from the sp package and recommends functions from base R, sp, gstat, and ggplot2 packages.

Uploaded by

Geta Tiruneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

R Cheat Sheet for the EthioSIS Crash Course on DSM

Dennis Walvoort & Dick Brus


December 2012

Introduction Organizing data


This document gives a brief summary of R- Data are usually stored in objects like data.frames
functions that may be useful during the crash and matrices. In our course, most objects have been
stored as Spatial*-objects as defined in the sp-
course digital soil mapping. package. For instance, map_ata is an object of class
SpatialPointsDataFrame. It can be converted to a
data.frame by means of:
Help
d <- as.data.frame(map_ata)
The most important functions are probably those
related to documentation and examples. To get ac- The number of rows an columns of this data.frame
cess to the documentation about function lm simply can be extracted by means of
type:
nrow(d)
help(lm)
## [1] 1197
or
ncol(d)
?lm
## [1] 22
If you don’t know the function name in advance,
just type a keyword with ?? as prefix: dim(d)
??kriging
## [1] 1197 22

or consult the internet! The first 5 rows and first 8 columns can be obtained
by means of

Packages d[1:5, 1:8]

All functions and data sets in R are organised in ## long lat pH OM ECEC N Al B
packages. Packages are organised in libraries. A ## 1 35.56 10.67 5.72 4.58 14.28 0.10 1610 0.10
package can be attached to your R-session by call- ## 2 35.79 12.28 6.98 2.79 45.61 0.09 766 0.10
ing the library function: ## 3 35.81 12.28 6.76 3.37 41.87 0.15 774 0.10
## 4 35.82 12.27 6.78 2.70 44.15 0.11 867 0.10
library(ethiosis) # attach ethiosis-package ## 5 35.83 12.28 7.19 3.65 25.98 0.11 666 0.26

An overview of row and column names is given by


names(d)[1:5] # first 5 column names
Loading data
## [1] "long" "lat" "pH" "OM" "ECEC"
R provides many functions to load data sets, e.g.,
read.table and read.csv. Spatial data can be row.names(d)[1:5] # first 5 row names
loaded by means of functions in the sp, rgdal or
## [1] "1" "2" "3" "4" "5"
maptools packages.
Precompiled data can be loaded by means of the Columns can also be accessed by name:
data function. For our course, several data files
have been precompiled in the ethiosis package: d$K[1:5] # first 5 rows of column 'K'
# attach ethiosis package
## [1] 48 111 115 135 207
library(ethiosis)

# consult its documentation


?ethiosis
Stats and maths functions
# load soil data
data(map_ata) R contains many statistical and mathematical func-
tions, e.g., mean, min, max, range, median, var,

1
cor, log, sqrt. See the help files for more details. Variography and Kriging
An example
a <- 3
A useful package for geostatistical analysis is the
a gstat-package. The following functions are useful
for our course:
## [1] 3
variogram: variogram estimation
b <- a + 1:5
b fit.variogram: variogram modelling
## [1] 4 5 6 7 8 krige: various types of kriging (and simulation)
sqrt(b) krige.cv: cross-validation
## [1] 2.000 2.236 2.449 2.646 2.828

The arrow <- is the assignment operator in R.


Plotting
Columns in a data.frame can be accessed and
modified as follows: R contains several packages for plotting, ranging
from simple base plots (graphics package) to ad-
d$log_K <- log(d$K) # adds a new column vanced plots (ggplot2-package). For our course,
d$log_K[1:5] # shows the first 5 rows of it
the following functions may be useful:
## [1] 3.871 4.710 4.745 4.905 5.333
hist: histograms
plot: scatter plots, box-whisker plots, etc.
Spatial functions and methods pairs: scatter plot matrices
In our course the focus is on spatial data. All data spplot: maps
sets in the ethiosis-package are objects of classes
defined in the sp-package. The following functions
might be useful: More information
coordinates: extracts coordinates Consult the internet:
proj4string: extracts projection attributes
• http://cran.r-project.org/manuals.html
Other methods/functions behave similar as those
for data.frames: • http://cran.r-project.org/other-docs.
html
ncol(map_ata)
• http://www.rstudio.com/ide
## [1] 20
• http://ggplot2.org
nrow(map_ata)

## [1] 1197

dim(map_ata)

## [1] 1197 20

Linear Regression
Linear regression can be done by means of the lm
function. In addition, many functions are available
to extract useful information from lm objects:
AIC: Akaike’s An Information Criterion
fitted: extracts model fitted values
residuals: extracts model residuals
influence: regression diagnostics
predict: model predictions

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy