Environmental Data Analysis and Visualization

Week 1 Lecture 2

Sensor of the day

(Photo credit: Shenandoah National Park via CBC.ca)

Sensor of the day

Sensor of the day

Sensor of the day

Sensor of the day

Movebank

https://www.movebank.org/

Checking in

  • How did it go yesterday?

  • How much do you feel like you remember?

  • Ready to keep going?

Introducing R scripts

An R script is a text document that contains lines of R code that can be delivered individually or collectively to the command line.

varA<-c(1:500)
varB<-runif(500,1,100)

data<-data.frame(varA,varB)

plot(data$varA,data$varB,type="l",col="orchid2")

Introducing R scripts

An R script is a text document that contains lines of R code that can be delivered individually or collectively to the command line.

Introducing R scripts

Scripts are text files, so they can be written, saved, and shared relatively easily.

varA<-c(1:500)
varB<-runif(500,1,100)

data<-data.frame(varA,varB)

plot(data$varA,data$varB,type="l",col="orchid2")

Activity

Open RStudio, go to the File menu, and create a new script. In that script, add some code:

  • Create a variable called myName and assign your name as a character string

  • Create a second variable called introduceMe and assign the character string “Hi, my name is”. Be sure to include a space at the end of the character string.

  • Use these two variables as arguments in the paste function.

Highlight all of the code in the script and click Run.

Comments

Comments are text preceded by a comment character. For R, the comment character is #. When R receives this at the command line, it knows not to run it.

#here's where we make some data
varA<-c(1:500)
varB<-runif(500,1,100)

#here's where we put it together in a dataframe
data<-data.frame(varA,varB)


#here's where we plot that data
plot(data$varA,data$varB,type="l",col="orchid2")

Comments

Comments are useful for documenting what a piece of code does, both for you and for other users. They can also be helpful for temporarily taking out a piece of code from a script without having to delete it.

#here's where we make some data
varA<-c(1:500)
#varB<-runif(500,1,100)
varB<-rnorm(500,0,1)  #Trying out a normal distribution here

#here's where we put it together in a dataframe
data<-data.frame(varA,varB)


#here's where we plot that data
plot(data$varA,data$varB,type="l",col="orchid2")

Getting help

My recommended approach to problems you might encounter is

  1. try and change a few things, and if that doesn’t work

  2. check the Help documentation, and if the answer isn’t there

  3. search for a solution on the web, and if that fails

  4. ask someone who can help you

Accessing help documentation

Help documents describe what functions do, what arguments they take, and what kind of objects they will return.

You can search help using the Help tab in the Outputs pane.

Accessing help documentation

You can also quickly look up a function’s help document by typing it into the command line preceded by a question mark (?). For example:

?hist

Reading the help documentation

Reading the help documentation

Help documents include:

  • Description What does the function do

  • Usage How do you call it in R?

  • Arguments What arguments does it take and what objects should be used?

  • Details Specifics about what different arguments do

  • Value

  • Examples Code showing how the code is used

Activity

Try looking at the entry for some of the functions we’ve used so far. Examples include:

  • rep

  • paste

  • scan

  • rnorm

See if you can use help to find the function for the Kolmogorov-Smirnov Test.

Going forward

  • Finding and importing data

  • How to handle different kinds of files and organize projects

  • More on what to do when things go wrong