Big Data 2 Introduction To R: Based On Prabhpreet Sidhu's Slides
Big Data 2 Introduction To R: Based On Prabhpreet Sidhu's Slides
Introduction to R
32
DSS/OTR
Workspace tab (1)
The workspace tab stores any object, value, function or anything you create during your R
session. In the example below, if you click on the dotted squares you can see the data on a
screen to the left.
DSS/OTR
History
tab
The history tab keeps a record of all previous commands. It helps when testing and running
processes. Here you can either save the whole list or you can select the commands you want
and send them to an R script to keep track of your work.
In this example, we select all and click on the “To Source” icon, a window on the left will
open with the list of commands. Make sure to save the ‘untitled1’ file as an *.R script.
35
DSS/OTR
Changing the working
directory
1
getwd()
# Changes the wd
setwd("C:/myfolder/data")
3
36
DSS/OTR
Setting a default working
directory
1
2
3
37
DSS/OTR
R script (1)
The usual Rstudio screen has four windows:
1. Console.
2. Workspace and history.
3. Files, plots, packages and help.
4. The R script(s) and data view.
The R script is where you keep a record of your work. For Stata users this would
be like the do-file, for SPSS users is like the syntax and for SAS users the SAS
program
38
DSS/OTR
R script (2)
To create a new R script you can either go to File -> New -> R Script,
or click on the icon with the “+” sign and select “R Script”, or simply press
Ctrl+Shift+N. Make sure to save the script
39
DSS/OTR
Packages tab
The package tab shows the list of add-ons included in the installation of RStudio. If
checked, the package is loaded into R, if not, any command related to that package
won’t work, you will need select it. You can also install other add-ons by clicking on
the ‘Install Packages’ icon. Another way to activate a package is by typing, for
example, library(foreign. This will automatically check the --foreign
package (it helps bring data from proprietary formats like Stata, SAS or SPSS).
DSS/OTR
Installing a package
Before
1
• We are going to install the package –
rgl (useful to plot 3D images). It
does not come with the original R
install
• Click on “Install Packages” 2
• write the name in the pop-up window
and click on “Install” 3
After
41
DSS/OTR
Plots tab (1)
42
DSS/OTR
Plots tab (2)
43
DSS/OTR
Plots tab (3) – Graphs export
To extract the graph, click on “Export” where you can save the file as an image (PNG, JPG,
etc.) or as PDF, these options are useful when you only want to share the graph or use it in a
LaTeX document. Probably, the easiest way to export a graph is by copying it to the clipboard
and then paste it directly into your Word document.
3
Make sure to select ‘Metafile’
4
15
5
DSS/OTR Paste it into your Word document
3D graphs