1. INTRODUCTION TO R: PART 2
Presenter: Faith Musili
F.Musili@cgiar.org
ICRAF - Geoscience Lab
29th September 2016
2. Creating a new project
The major purpose of creating a project is to organize your
work for easy revisit later, a project may include as many
scripts as one wishes .
Procedure:
File
New project
New Directory
Empty Project
Then a popup appears
3. Cont.…..
Give your new directory a name and browse to the
destination of your choice.
Create project
NB: Incase you wish to open the new project in another
session to avoid interrupting the current session , tick the
box at the bottom.
4. Creating a new R script
window
In the new created project , follow the steps below to
create a new script window;
File
New File
R Script
Save the r script in the destination of your choice ,by
clicking the save icon at top left corner of scripting
window.
5. Set working directory
You can either use double backslashes for windows or a single forward slash for mac.
Examples:
Mac
setwd("/Users/FMusili/Documents/2.2")
Windows
Then, click the run button at top right corner of scripting window. Note that in Mac the
shortcut (control+return) and in windows (control+enter) can be used to run a line of code.
6. Points to note in R scripting
1. No spaces should be used while writing file paths ,instead replace
the spaces with underscores.
2. R syntax is case sensitive.
3. Comment sign "#" is placed at the start of sentences or lines of
code that we don't wish to run.
#####Setting working directory
setwd("/Users/FMusili/Documents/2.2")
Or
setwd("/Users/FMusili/Documents/2.2")###Setting working directory
7. Creating objects in R
n <- 150
n
58 -> n
n
x <- 0.3
x
X <- 108
X
n<-3+sqrt(10)
n
8. Basic R syntax
1. Setting the working directory setwd()
2. Knowing which working directory you are presently in
getwd()
3. Installing new packages install.packages("...”)
4. Loading an already installed library library()
5. Knowing which R version you are using version
6. Read more about a function ??....
9. R packages
Packages are collections of R functions, data, and compiled
code in a well-defined format.
R comes with a standard set of packages. Others are available
for download and installation. Once installed, they have to be
into the session to be used.
Example of downloadable packages:
Dplyr and tidyr for data manipulation
Rgdal and raster for spatial data handling
Igraph for network analysis etc.