1100 01 R Intro Video
1100 01 R Intro Video
R and RStudio
Dr. ZHOU Titi
Division of Social Science
HKUST
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 0/40
Installing R & RStudio RStudio R Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 1/40
Installing R & RStudio RStudio R Conclusion
Why R?
J Rcabopeeudeand
! Very limited in terms of what analyses they allow you to do.
! Not facilitate robust and reproducible practices. ,
E7 X
Khem how to Ereate amad use the data
! A very good idea to avoid proprietary software. result
yMath
! While glossy and powerful, they are also very expensive.
! R: open-source, freely available for Windows, Mac and Linux.
Installing R
How to install R?
! Download at http://cran.r-project.org/.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 3/40
Installing R & RStudio RStudio R Conclusion
Installing R
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 4/40
Installing R & RStudio RStudio R Conclusion
Installing R
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 5/40
Installing R & RStudio RStudio R Conclusion
Installing R
! R comes preinstalled on many Linux systems, but you’ll want the latest
version.
! "Download R for Linux" ⇒ files to build R from source on Debian, Redhat,
SUSE, and Ubuntu systems.
! The installation procedure varies with the Linux system you use.
! CRAN provides separate instructions by grouping each set of source files with
documentation or README files that explain how to install on your system.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 6/40
Installing R & RStudio RStudio R Conclusion
Installing R
Testing R
plot(1:10)
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 7/40
Installing R & RStudio RStudio R Conclusion
Installing RStudio
Why RStudio?
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 8/40
Installing R & RStudio RStudio R Conclusion
Installing RStudio
! Once installed, you can open it ( ) like any other program on your
computer.
! Note: you must install R before you install RStudio.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 9/40
Installing R & RStudio RStudio R Conclusion
How to update R?
! Usually issue updates every six months. Find the latest version to download.
! For Windows users, you may need to uninstall R to update it. You can find
a quick guide here.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 10/40
Installing R & RStudio RStudio R Conclusion
1 Click "Help" on the top menu bar ⇒ click "Check for Updates".
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 11/40
Installing R & RStudio RStudio R Conclusion
RStudio orientation
! When you start RStudio for the first time, you’ll see three panels:
Console
Console
! What does [1] mean? For now, you can think of it as if R were saying "the
answer to the 1st question you asked is 4".
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 13/40
Installing R & RStudio RStudio R Conclusion
Console
! In the lecture slides, code and its output will be presented in a way that is
different from what you will see in the Console.
! In your Console, you type after the > , but the code in our slides will not
contain this command prompt.
! In your Console, output appears directly after your code, while in the slides,
it is commented out with ## .
! To let you be able to copy code from the slides directly into R for testing.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 14/40
Installing R & RStudio RStudio R Conclusion
Console
10 = 20
## Error in 10 = 20: invalid (do_set) left-hand side to assignment
! R is mindlessly obedient and can only do exactly what you ask it to do.
You absolutely must be precise in what you say to R.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 15/40
Installing R & RStudio RStudio R Conclusion
Console
citat ion()
## Error: unexpected symbol in "citat ion"
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 16/40
Installing R & RStudio RStudio R Conclusion
Scripts
Scripts edier
! The Console is great for simple tasks but not for projects with a lot of code.
! R script: a plain text file with a .R file extension
! It stores your lines of R code and allows you to edit. save thecode
! The lines of code are to be sourced into the R Console line by line.
! ALWAYS work from a script file in this class.
! To create a new script, use the menu or the toolbar button as shown below.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 17/40
Installing R & RStudio RStudio R Conclusion
Scripts
Now you have a new window (called the Source pane) in the top left of RStudio.
! The new window is a script editor and where you will write your code.
! The Console is now in the bottom left.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 18/40
Installing R & RStudio RStudio R Conclusion
Scripts
2 Multiple lines: Select the lines you want to run and hit command+enter /
ctrl+enter , or use the Run button.
3 Entire script: Use the Source button.
ctnltF
savefilelcommand 7
clickFile- 7save → .
.
.
.
.
. . . . .
. . . .
. . . .
. . . .
. . . .
. . . . .
.
.
.
.
.
.
.
.
.
19/40
Installing R & RStudio RStudio R Conclusion
Other Windows
Environment/History/Connections ( f: le 仔)
! The "Environment" tab: 7 Save the object
! All the objects (to be discussed later) you have created in the current
(global) environment are displayed here.
! Objects can be displayed as a List ( ) or in Grid ( ) format by
selecting your choice from the drop down button on the top right of the
window.
! In the Grid format, you can remove objects from the environment.
! The "History" tab:
! It contains a list of all the commands you have entered into the R Console.
! You can search back through your history for the line of code you have
forgotten, send selected code back to the Console by double clicking the line.
! We usually never use this as we always refer back to our R script.
! The "Connections" tab:
! It allows you to connect to various data sources such as external databases.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 20/40
Installing R & RStudio RStudio R Conclusion
Other Windows
Files/Plots/Packages/Help/Viewer
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 22/40
Installing R & RStudio RStudio R Conclusion
1 Do calculations
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 23/40
Installing R & RStudio RStudio R Conclusion
Doing calculations
1. Do calculations
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 24/40
Installing R & RStudio RStudio R Conclusion
Doing calculations
5ˆ4
## [1] 625
4/2*3
## [1] 6
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 25/40
Installing R & RStudio RStudio R Conclusion
Creating objects
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 26/40
Installing R & RStudio RStudio R Conclusion
Creating objects
my_result <- 10 + 4
my_result ## [1] 14
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 27/40
Installing R & RStudio RStudio R Conclusion
Creating objects
! Calculate on an object:
! Both objects appear in the
Environment pane in RStudio.
my_result * 2 ## [1] 28
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 28/40
Installing R & RStudio RStudio R Conclusion
Creating objects
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 29/40
Installing R & RStudio RStudio R Conclusion
3. Vectors
! Vector: a set of information contained together in a specific order.
! Normally numbers, but often also character strings or logical elements.
check
Working with vectors
() (
Vector indexing: [ ]
弟年但 number 是什麼
! The order in which the numbers in the vector are stored is important.
! Indexing: to access specific elements of a vector.
! To access an individual element of a vector, use square bracket [ ].
my_first_vector
## [1] 0 1 1 2 3 5 8 13 21 34
my_first_vector[ 3 ]
## [1] 1
my_first_vector[ c(1,3,4,5) ]
## [1] 0 1 2 3
element
Cun check pant of the
.
.
.
.
.
. . . . .
. . . .
. . . .
. . . .
. . . .
. . . . .
.
.
.
.
.
.
.
.
.
31/40
Installing R & RStudio RStudio R Conclusion
! Use the minus sign "– " to remove the corresponding element from a vector:
my_first_vector[ - c(8:10) ]
## [1] 0 1 1 2 3 5 8
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 32/40
Installing R & RStudio RStudio R Conclusion
! Without the " ", R thinks that sosc1100 is the name of an object and R is
right; there is no object called sosc1100 in the environment.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 34/40
Installing R & RStudio RStudio R Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 35/40
Installing R & RStudio RStudio R Conclusion
! After running the code above, class will contain the text "data analysis"
instead of "sosc1100"
! R is case sensitive:
! class is different from Class.
Class
## Error: object ’Class’ not found
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 36/40
Installing R & RStudio RStudio R Conclusion
2 + 2 == 5
## [1] FALSE check the statemeht
! We use the equality operator == to force R to make a "true/false"
judgment.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 37/40
Installing R & RStudio RStudio R Conclusion
cnly
貪 savenoinenviunmet
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 38/40
Installing R & RStudio RStudio R Conclusion
x <- c( 2, 8, 4, 10, 5, 3, 9)
x>5
## [1] FALSE TRUE FALSE TRUE FALSE FALSE TRUE
sarrtheststeniat
! We can store the output in a vector y:
y <- x>5
y ## [1] 空但的 tiyt
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 39/40
Installing R & RStudio RStudio R Conclusion
Today’s Lecture
! RStudio: R script, R console, and the environment
! How to use R:
1 Do calculations: +, −, ∗, /, ˆ
2 Create objects: <-,
3 Working with vectors: c(), [ ], " ", =, ==
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . 40/40