R for Spatial Statistics

 

Reference

Incomplete

This is a reference page with short descriptions of the most commonly used commands in R for spatial statistics.

Data Types (Modes)

mode, namely numeric, complex, logical, character and raw

x =1.2345 # float 
small= 1.0e-10 # float using scientific notation 
species= 'Pinus contorta' # string 
conifer= TRUE # boolean

Return TRUE or FALSE:

is.numeric(Variable)
is.character(Variable)
is.vector(Variable)
is.matrix(Variable)
is.data.frame(Variable)

Return a new value of the specified type:

 as.numeric()
as.character()
as.vector()
as.matrix()
as.data.frame) 

Get the type, also known as "class" for primitive types:

typeof(elev) # returns type of value 

Special Values

is.finite(x)  
is.infinite(x)  
is.nan(x) 

Values:

Inf # infinite  
NaN # not a number

Not available:

z=NA # sets Z to not available
is.na(z) # true if value not available 

Vectors

Generating Vectors

x = c(10.4, 5.6, 3.1, 6.4, 21.7) # vector with the specied values
s3 = seq(-5, 5, by=.2) # vector from -5 to 5 in increments of 0.2
s4 = seq(length=51, from=-5, by=.2) # 51 numbers from -5 in increments of 0.2
s5 = rep(x, times=5) # repeats the contents of x 5 times
x = 2:16 # creates an array with values from 2 to 16

Accessing Vectors

y=x[0] # first entry
y=x[1:2] # y will contain 2 entries, starting at entry 1
y=x[-3] # remove the 3rd entry
length(alpha) = 3 # changes the length of the sequence to 3 

Utility Functions

sort(x)
length(x) # number of entries in x

Appling Functions to Vectors

Vector=apply(Data,DimensionIndex,Function) # returns an array from appling the function to the data

Matrix Mathematics

x = y %*% z # matrix multiplication 
solve(A)  
eigen(Sm) # eigen values 

Math

exp(x)

max(x)
min(x)
sum(x)
mean(x)

Random numbers:

x=runif(100) # 100  random numbers from 0 to 1 with uniform distribution

 

Matrices

veg[c(0,1,2,3),c(6,7,8,9)]

 

Stats Tests

anova(bere4.glm,test="Chi")

t.test(elev~depth,var.equal=FALSE)

Plots

Plots

hist(x,probability=TRUE,col=gray(.9),main="Random Numbers from 0 to 1") # histogram
qqnorm(x) # QQ plot
cov(vector1, vector2) # covariance

boxplot(elev~depth)

Data Frame

na.omit(DataFrame) # remove values that are "not available" (na). 
nrow(DataFrame) # returns the number of rows in the data frame
ncol(DataFrame) # returns the number of columns in the data frame
dim(DataFrame)          # to get the dimensions of the data frame
names(DataFrame)        # to get the column names  
row.names(DataFrame)    # to get the row names (plots in our case)

DataFrame[1, 2] # Returns the entry in the first row, second column
DataFrame[1,] # Returns the first row
DataFrame[,1] # Returns the first column

Files

The following functions read data from csv (or other delimited files) into a data frame.

TheData=read.csv(FilePathWithFileName.csv,header=T)
TheData=read.delim(FilePathWithFileName, header = TRUE, sep = "\t", qmethod = "\"") 
TheData=write.csv(DataFrameToWrite,FilePathWithFileName.csv,header=T)
TheData=write.delim(DataFrameToWrite,FilePathWithFileName,sep = "\t", qmethod = "\"") 

Models

y ~ x - for modeling y dependent on x

Linear Regression

summary(model)
RegressionModel=lm(cars$dist~cars$speed)
abline(RegressionModel)

AIC(bere3.glm,glm(berrep>0~elev,family=binomial))
glm( formula, family=binomial(link=probit))

Control Flow

If Statements

if (x<12) print("X is less than 12")

While Loops

i=0  
while(i<100) { i=i+1 }

For Loops

N=100  
for(i in 1:N) { print(i) }

Functions

PrintSeries = function (N)   
{  	
	return(N)
}

Other Resources

R Documentation: Vectors