4. R Studio
R Basics
Operators
Packages
Importing
DataCamp
R software
R is a free software for statistical analysis, text mining and
graphics.
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
4 / 50
5. R Studio
R Basics
Operators
Packages
Importing
DataCamp
R Studio
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
5 / 50
7. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Organizing Your Files
Option 1
Create new script / Open existing script
Set up your working directory
Keep your datafiles in this directory (easy access)
Or use command file.choose()
Or remember the path to datafiles
7 / 50
8. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Organizing Your Files
Option 1
Create new script / Open existing script
Set up your working directory
Keep your datafiles in this directory (easy access)
Or use command file.choose()
Or remember the path to datafiles
Option 2
Create new project/ Open existing project
Do not have to set up working directory
Keep your datafiles in the project directory
Do not have to remember the path to datafiles
7 / 50
16. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Learning R Syntax
variable stores values
Assignment operator: <-
x <- 5
y <- 6
A valid name for variable must start with a letter.
Name can contain letters, numbers, underscores, and dot.
Valid names Invalid names
mydata
my data
mydata2
my.data
mydata!
my data
2mydata
.mydata
14 / 50
17. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Script Flow
1 Create two variables
x <- 5
y <- 6
2 run executes commands:
- Place cursor anywhere on the first line - click run
- Place cursor on the second line - click run
3 Console displays the execution
4 Right top
- Environment stores objects
- History stores commands
15 / 50
22. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Characters versus Numeric Values
Numbers are without quotation marks:
x <- 5
Characters are enclosed in quotation marks:
z <-“a”
Arithmetic operations with numerics
In the console type x*y, press enter
In the console type z*w, press enter
19 / 50
28. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Index Slicing
1. [1:3] - consecutive elements: one, two, three
2. [c(1,3)] - only the elements one and three
3. [-2] - all except the element number two
Extract the first and the second elements
Extract all except the first element
Extract the first and the fourth elements
25 / 50
29. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Indexing
How to extract certain elements from a vector?
What is the first word in mywords?
- mywords[1]
What are the first and second words in mywords?
- mywords[1:2]
What are the first and third words in mywords?
- mywords[c(1,3)]
26 / 50
35. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Usefulness of paste - Create a Plot Title
Scenario: You are going to create a plot with x (Age Groups)
and y (Frequency) with the following title
My plot: Frequency of Age Groups
y <- "Frequency"
x <- "Age Groups"
title <- "My plot:"
c(title,y,"of",x)
30 / 50
36. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Usefulness of paste - Create a Plot Title
Scenario: You are going to create a plot with x (Age Groups)
and y (Frequency) with the following title
My plot: Frequency of Age Groups
y <- "Frequency"
x <- "Age Groups"
title <- "My plot:"
c(title,y,"of",x)
paste(title,y,"of",x,collapse=" ")
30 / 50
38. R Studio
R Basics
Operators
Packages
Importing
DataCamp
List versus Vector
Vectors contain the objects of the same type:
- v1 <- c(“a”,“b”,“c”)
- v2 <- c(1,2,3,4)
Lists contain different types of objects
Vector uses c() function
List uses list() function
Create mylist:
32 / 50
39. R Studio
R Basics
Operators
Packages
Importing
DataCamp
List versus Vector
Vectors contain the objects of the same type:
- v1 <- c(“a”,“b”,“c”)
- v2 <- c(1,2,3,4)
Lists contain different types of objects
Vector uses c() function
List uses list() function
Create mylist:
miniquiz: What are the data types in mylist?
32 / 50
43. R Studio
R Basics
Operators
Packages
Importing
DataCamp
Indexing List
1 Print list: print(mylist)
2 Remember vector indices [ ]?
3 List will use [[ ]]
4 Type mylist[[1]]
5 Type mylist[[7]]
6 How to access the first number
inside the list object?
7 mylist[[7]][1]
33 / 50