0% found this document useful (0 votes)
2 views7 pages

Introduction

Uploaded by

ninakelliaumwali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Introduction

Uploaded by

ninakelliaumwali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INTRODUCTION TO R TRAININGS

Rafiki Murenzi

26. April 2024

Contents

Outlines: 2

Section 1: Introduction to R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Section 2: R objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Section 3: IF statements, functions and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Section 4: DataFrames manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Section 5: Data Visualization with ggplot2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Section 6: Geospatial Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1. Introduction to R 3

1.1 Why choose R? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2 Here are advantages of R: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.3 R Studio interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.4 CRAN, library, packages, and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

CRAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

What’s on your bookshelf? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Installing Packages on a Personal Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1
Research proposal Rafiki Murenzi

Outlines:

Section 1: Introduction to R

• Getting started
• R Studio interface

Section 2: R objects

• Creating Vectors and Matrices in R

• Factors in R: Categorical & Continuous Variables


• Difference between Table, DataFrames and array

Section 3: IF statements, functions and Loops

• IF, ELSE, ELSE IF Statement in R.


• Create Functions in R
• Loops in R

• Exercises

Section 4: DataFrames manipulation

• Import Data into R: Read CSV, Excel, SPSS, Stata…


• R Data Frames: Create, Append, Select, Subset, Sort
• Dealing with Missing Values(NA) in R
• R Dplyr Tutorial: introduction to dplyr

• R Dplyr Tutorial: Aggregate Function


• Dplyr Tutorial : Merge, join, append and reshape in R
Research proposal Rafiki Murenzi

Section 5: Data Visualization with ggplot2

• ggplot2 syntax
• Axis labels and Legends

Section 6: Geospatial Graphs

• Introduction to “ggmap” and “sf”


• Practical exemple on Geospacial map

1. Introduction to R

R is a powerful and flexible tool with a big and active community of users and developers that constantly
posts in blogs and forums.

1.1 Why choose R?

• It is way cooler.
• It is less specialized

• More flexibility when programming


• many other functionalities.
• Much broader network of users in general

• Development of new features and bug fixes happens faster.


• It makes prettier graphs.

1.2 Here are advantages of R:

• R is a free and open source software!


• It allows you to have several data sets open simultaneously.
• It can run complex Geographic Information System (GIS) analyses.
Research proposal Rafiki Murenzi

• You can use it for web scrapping.


• You can run machine learning algorithms with it.
• You can create complex Markdown documents. This presentation, for example, is entirely done in
RStudio.

• You can create dashboards and online applications with the Shiny package.

1.3 R Studio interface

When RStudio is open at the beginning it has 4 different windows: Console window, code editer, environment
or histories and Packages/Help/plots etc…

Figure 1: R data types.


Research proposal Rafiki Murenzi

• Code Editor:script, data, command to run script.


– This is the panel you’re reading this tutorial in. It contains the script editor where we can create
and edit R Notebook files, among other files.
• Console: console for running scripts.
– This is the Console Panel where R code is passed to and executed.
• Environment/History: environment
– The environment tab keeps track of variables we’ve created in this workspace.
• Files/help etc: files, plots, packages, help
– This is a multi-purpose panel which contains:
∗ Files: A basic file explorer,
∗ Plots: Where plots can be rendered,
∗ Packages: install and import libraries into R,
∗ Help: Explorer for Documentation of functions and libraries,
∗ Viewer: View local web content e.g. Shiny app.

1.4 CRAN, library, packages, and functions

So far, we’ve seen R’s capabilities as a large calculator and also as a place for storing objects and vectors.
However, it is much much more than that! One of the things that makes R amazing is the open source
community surrounding it.

The R community which is made up of academics, statisticians, social and political scientists, economists,
and data scientists to name a few, are responsible for authoring a wide variety of packages (> 15,000) that
can do a wide range of data manipulation, visualisation, and analysis tasks.

To get your head around what CRAN, library, packages, and functions are I find it helpful to think of books.

CRAN

CRAN stands for the Comprehensive R Archive Network. It’s like the R equivalent of the British Library
or Library of Congress. It holds a copy of every package (book) and all the versions of R.

Library

On your computer you’ll have a local library with copies of the packages you’ve installed from CRAN (your
home office book shelf).
Research proposal Rafiki Murenzi

What’s on your bookshelf?

• Click on the ‘Packages’ tab in the lower right hand panel (Q4 from before). You can see what packages
are in your library, a short description of what they do, and the package version.

• The packages that are loaded have a check mark in the box on the left. As before, there are several
packages that are automatically loaded each time you start an R session, e.g. base package.
• Although it is possible to load and install your packages from here, I recommend using the functions
shown below instead. This way, someone else or future you knows exactly what packages they need
to run the analyses.
• You should load the packages you will use at the top of your script, so that future you or your colleague
knows what needs to be installed/loaded.

Installing Packages on a Personal Computer

A package needs to be installed only once and requires an internet connection which allows your computer
to communicate with the CRAN server. There is two ways to install a package On your personal computer.

The first is to install a package to your local library from CRAN by uncommenting and running the following:

”[Link](PackageName)”.

You may wish to install a package with the additional argument: dependencies = TRUE, this will also install
any packages that the package depends on.

”[Link](PackageName,dependencies=TRUE)”

The second way is to use the RStudio interface as follow.

1. Run R studio.

2. Click on the Tool tab in the upper-right section and then click on install packages. The following
dialog box will appear.
Research proposal Rafiki Murenzi

3. In the Install Packages dialog, write the package name you want to install under the Packages field
and then click install.

Figure 2: Install Packages in R.

You might also like