Geostatistics SoSe 2015 R tutorial

Things you need before you start with this tutorial

  1. Download and install R on your system

Bingo! You are all set.

How to R your homeworks

Now, a how to for Homework 1.

Open R on your system. On my system (MacOS X) it looks like this:

Figure 1: R running on a Mac OSX Terminal.

There, where the cursor (the gray rectangle) is you can start typing R commands. R will process your commands and will print results to the Standard Output (STDOUT; i.e. the screen) or will store the results in objects.

To read data into R type:


This will read the datafile and will output the data to the STDOUT. So it will print the contents of the datafile to screen in the same way the unix command line cat does. After printing the data, the data is gone.

So... How can you work with it? you have to store the data somewhere in R. This somewhere is more a something and we call it an object. In R, we store data in objects. The read.csv command is reading the data and returning an object which ends up in STDOUT because there is nowhere to store it.

You “store” objects (the correct term is assign, I think) using the left arrow: <- . For instance:


Here, you are assigning the object returned by the method read.csv to the variable myData . And you can use the data for other purposes in R by calling or passing myData to other methods as we will see next.

Note that variable names can only contain alphanumeric characters, no spaces or special characters like ,@!\%() are allowed.

As everything in R, read.csv has an extensive help and several options you can use. To check these options type:


This command will display read.csv help page.

Ok. Now lets start with Homework 1.

First thing you need to know where your data is. In my system, I have all the Geostatistics data in my repositories folder which I can find in my home directory (/home/sevra ). The direction of your datafile is called the path. In my case it is /Users/sevra/Repos/Geostatistics/Data/10.5061.dryad.r2170/Yasuhara_Tab2-USNM.csv . In your case, the path to the file will look different.

Now you can load the data into R using the read.csv command we just used before. In R, type:

>homework_1_data<-read.csv("/Users/sevra/Repos/Geostatistics/Data/10.5061\/dryad.r2170/Yasuhara_Tab2-USNM.csv", head=T)

Question: what is the option head=T doing?1.

Ok. Now you have an object in R storing the data you need to complete homework 1.

Say you want to take a look at the data you just uploaded to R, type >head(homework_1_data) to ask R to display the first lines (i.e. the head) of your data matrix2. Say you don’t know anymore which objects you have in your Workspace3, type >ls() to tell R to list them. Or say you want to remove an object you don’t need anymore, type >rm(name_of_object_to_delete_here) to tell R you want to delete something4.

Good. Now, what happens if only want to know the names of the variables (columns) in my data matrix? You type >names(homework_1_data) and voila, you get them.

  1. Remember you can read the help every now and then

  2. This is convenient if you work with large matrices