2. What is R programming language?
• Open source software enviroment for statistical computing and
graphics.
• Object-oriented language i.e we create objects and manipulate them
as intended.
• Functional programming language (it provides many tools for the
creation and manipulation of functions) written primarily in C and
Fortran.
• It's flexible, extensible and comprehesive for productivity.
5. •
• ICRAFuseR slack channel
• RLadies groups
• Africa R group
• Nairobi R group
• Twitter
R user communities and groups
6. • A user interface
• An intergrated
development
enviroment (IDE)
What is Rstudio?
NHSLinf
7. 1. Downloading and installing R and Rstudio
To Install R
1. Open an internet browser and go to www.r-project.org.
2. Click the "download R" link in the middle of the page under "Getting Started.”
3. Select a CRAN location (a mirror site) and click the corresponding link.
4. Click on the "Download R for (Mac) OS X" / "Download R for Windows”/ "Download R for Linux" link
under Download and Install R.
5. Click on the file containing the latest version of R .
6. Save the file, double-click it to open, and follow the installation instructions.
7. Now that R is installed, you need to download and install RStudio.
To Install Rstudio
1. Go to www.rstudio.com and click on the "Download RStudio" button
2. Click on "Download RStudio Desktop.”
3. Click on the version recommended for your system, save the file on your computer, double-click it to
open, and then drag and drop it to your applications folder.
Where do I start?
8. What are they?
A package bundles together code, data, documentation, and tests, and is easy to share with others.
Who develops them?
Any R developer who wants to contribute to the open source community.
How to install?
1. install.packages(”name of package”)
2. Use the Rstudio interface i.e Packages ->install -> type name of package on popup -> install
How to use them in R
1. Load the package i.e library(name of package)
2. You can now start using the package functions
How to get help
1. Use package cheat sheets available online
2. In R console type ??function
R packages
9. Data frames
Vectors
a <- c(1,2,5.3,6,-2,4) # numeric vector
b <- c("one","two","three") # character vector
c <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) #logical vector
Matrices
y<-matrix(1:20, nrow=5,ncol=4) # generates 5 x 4 numeric matrix
Arrays. Are similar to matrices but can have more than two dimensions.
Lists
w <- list(name="Fred", mynumbers=a, mymatrix=y, age=5.3)
Factors
gender <- c(rep("male",20), rep("female", 30))
gender <- factor(gender)
Data types in R
10. 2. R resources and materials
1. Package cheat sheets
2. Rstudio help page ??function
3. R for data science book
4. Landscape portal blogs http://landscapeportal.org/blog/categories/r/
5. Data camp
6. Coursera
7. Other R users
8. Just google it
11. • Always create new folders for each project if applicable
• Always create “New projects” for projects
File New project New Directory Empty Project specify new
directory name and browse to a location on your computer. Create new project
• Set working directory setwd /Users/FMusili/ OneDrive - CGIAR /Scripts/Climatemodule/Climate”)
• Use relative paths (data/shp/Africa-bnd-outline.shp) instead of absolute paths (“/Users/FMusili/
OneDrive - CGIAR /Scripts/Climatemodule/Climate/data/shp/Africa-bnd-outline.shp”)
• Use short and comprehesive object names. Use a naming convention consistently.
• Use comments when writing scripts for easy re-visit by you or someone else “#”
• Use code management systems like github, gitlab, bitbucket etc. for version control and code
backup
• Pipe your code as much as possible -----to be illustrated later
• Practise! Practise! Practise! Practise! Practise! Practise! Practise! Practise! Practise!
Some good practices when programming in R
12. # My first program in R Programming
myString <- "Hello, World!"
print ( myString)
[1] "Hello, World!"
Basic R
n <- 15
n
[1] 15
5 -> n
n
[1] 5
x <- 1
X <- 10
x
[1] 1
X
[1] 10
m<-3+sqrt(10)
m
[1] 6.162278
R is case sensitive
(10 + 2) * 5
[1] 60
13. 2019
• Introduction to R on 24th October 2019 by Faith Musili
• Reading data in R on 21st November 2019 by Makui Parmutia
• Data manipulation in R (1) on 5th December 2019 by Aida Bargues-
Tobella
2020
….......
ICRAFuseR seminars for beginners