You are on page 1of 24

R Studio

R Basics

Packages

Datasets

R Reading Group
Introduction to R

Olga Scrivner

1 / 23
R software

R Studio

R Basics
R is a free software for statistical analysis, text mining and
Packages
graphics.
Datasets

To install R on Window:

1 Download the binary file for R https://cran.


r-project.org/bin/windows/base/R-3.3.1-win.exe
2 Open the downloaded .exe file and Install R

To install R on Mac:

1 Download the appropriate version of .pkg file


https://cran.r-project.org/bin/macosx/
2 Open the downloaded .pkg file and Install R

2 / 23
R Studio

R Studio

R Basics

Packages

Datasets

RStudio is a free user interface for R.

1 Install the appropriate RStudio version https:


//www.rstudio.com/products/rstudio/download/
2 Run it to install R-studio

3 / 23
R Studio Structure

R Studio

R Basics

Packages

Datasets

For more details - see handout RStudio101 (by Oscar


Torres-Reyna)
4 / 23
Creating Scripts

R Studio

R Basics

Packages
Create R File: File → New File → R Script
Datasets

Save R File: File → Save - name it (ex. myscript1.r)

Close R File: File → Close

Open R File: File → Open

5 / 23
Creating Files

R Studio

R Basics

Packages

Datasets

6 / 23
Learning R Syntax

R Studio

R Basics

Packages

Datasets
Assignment of a value to a variable is done with <- (no
space).

variable <- value

7 / 23
Learning R Syntax

R Studio

R Basics

Packages

Datasets
Assignment of a value to a variable is done with <- (no
space).

variable <- value

NB: Compare this structure to a factor and its values: factor


(variable) is a fixed name, while factor values can change.

gender <-“m”
gender <-“f”

7 / 23
Strings

R Studio

R Basics

Packages

Datasets

Strings (characters) are enclosed in quotation marks:

mystring <-“This is my string”

Numbers are without quotation marks:

mynumber <- 5

8 / 23
Run - Practice

R Studio

R Basics
To execute your commands you need to click run
Packages

Datasets

Open your R file


Type mystring <-“This is my string”
Keep your cursor on the same line and click run
Type print(mystring)
Look at your console (bottom left)

9 / 23
Vector

R Studio

R Basics

Packages

Datasets Vector is a sequence of strings (or numbers)

vector <- c(“string1”,“string2”,“string3”)

vector <- c(1,2,3,4)

Practice:

Type mywords <-c(“These”, “are”,“my”,“words”)


Keep your cursor on the same line and click run
Type print(mywords)

10 / 23
Length

R Studio

R Basics

Packages
Length of string vector - how many elements (words) are in
Datasets
the vector

mywords <-c(“These”, “are”,“my”,“words”)

1 How many words in mywords?


Type length(mywords) and Run
2 What is the first word in mywords?
Type mywords[1] and Run
3 What are the second and third words in mywords?
Type mywords[2:3] and Run

11 / 23
Installing Packages

R Studio

R Basics

Packages
In your bottom left window - go to Packages
Datasets

12 / 23
Selecting Packages

R Studio

R Basics

Packages

Datasets

13 / 23
Package = Library

R Studio

R Basics

Packages

Datasets In your Packages window scroll down until you see languageR
and click inside the box:

14 / 23
Package Content

R Studio
To access package description and its content, click on the
R Basics

Packages
package name.
Datasets

New window Help will open up:

15 / 23
Accessing Info from Packages

R Studio

R Basics
Scroll down and select languageR-package
Packages

Datasets

You will see the list of available functions from this package

16 / 23
Quick Help

R Studio

R Basics

Packages Type in the console (bottom left):


Datasets

?length
Instead of Run - click enter-key

17 / 23
Available Datasets

R Studio

R Basics

Packages
Go to Packages
Datasets

Scroll down until the dataset sleep

18 / 23
Dataset - Sleep

R Studio

R Basics

Packages

Datasets

Let’s load the dataset:


In your script type data(sleep)
Can you find the description of the function head? (tip -
use the question mark)

19 / 23
Working with Dataset

R Studio

R Basics

Packages

Datasets

Let’s look at the first column:

What are the column names?


colnames(sleep)
What is the name of the first column?
Type sleep$extra (with dollar sign)

20 / 23
Working with Dataset

R Studio

R Basics

Packages Find out the description of the function hist


Datasets
Type hist(sleep$extra)

21 / 23
Exporting and Saving

R Studio

R Basics

Packages

Datasets

Export graph
Save your script

22 / 23
Practice

R Studio

R Basics

Packages

Datasets

Next: loading files into R (text files, csv files, urls, twitter etc)

RRRRR!

23 / 23

You might also like