9. July 2012

# introduction to R: learning by doing (part 1)

Geography is often about statistics as it is the basis for fast exchange of information: providing a mean and standard deviation to the audience is often much easier then showing raw data: Learning a script language for this purpose can be a hard-ass work. But I think it is more often a need of practice. And by practice I mean typing, reading and trying out.

Type the code below into your R console to get some insight into the R syntax. If you need to install R beforehand follow this instruction

a <- 7 #a will be treated as a scalar and will get the value of 7 b <- c(1,2,3) #b is now a &quot;c&quot;olumn vector with three entries e <- c(1, a, 3) #whohoo look where the &quot;a&quot; is coming from f <- r(2,3,4) #nope, there are no row-vectors in R as far as I know f <- t(c(1, a, 3)) #this could be the problem solver but this is not the same as it is a matrix... e #will show you all entries in e e[2]#will give you the second element from e ls() #will show you all available variables rm(b) # let us &quot;r&quot;e&quot;m&quot;ove the be from our workspace

As we have already created a very easy matrix lets go a little further with matrices:

A <- diag(3) #will show you the identity matrix with ones on the diagonal A <- A * a #lets guess ... A[3,2] <- pi # will set the third row and second column with the value of pi X=matrix(1, nrow=3, ncol=10) # a mtrix with one as a the only value for 30 cells. E <- diag(e) #lets create a diagonal matrix of e #transposition of a matrix E = t(E); #with the semi-colon we are suppressing the output of the operation; and the "=" seves as "<-"

Lets get back to vectors as they are important for statistical calculations: normally a vector represents a list observations of one parameter only:

#let us do some vector creation: for (i in c(1:10)) { v[i] &lt;- i*2 } #will give you a vetcor with ten numbers ranging from 2 to 20 in steps of two v2 <- seq(2,20,2) #will do the same sequence 😉

Now: operating on matrices with R. keep in mind to think column wise as the splitting of the vector is done by columns first.

rm(list=ls()) #will remove everything from your workspace A <- matrix(c(1,2,2,1,3,0), nrow=2, ncol=3) B <- matrix(c(2,1,0,2,0,1), nrow=3, ncol=2) C <- matrix(c(0,5,1,1,0,3), nrow=2, ncol=3) dim(A) #will provide info about the numbers of rows and colums dim_A <- dim(A) #is fine as well A%*%B #multiplication A%*%C #failure due to &quot;misshape&quot; matrices A%*%t(C) #multiplication with the transpose diag(A%*%t(C)) #will give you the values along the diagonal of the resulting matrix D <- A+C #Matrix addition c(2,1,3) -> x # "<-" the other way around... still "<-" is not the same as "=" as "=" is without a "direction" g <- A*x

Try it out…