Chapter 1 Introduction to R
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 1 Introduction to R Math 3210 Dr. Zeng Outline I Introduction to R language I Introduction to RStudio I Introduction to R markdown Introduction to the R language The Package I R is a statistical computer program made available through the Internet under the General Public License (GPL). I R provides an enviroment in which you can perform statistical analysis and produce graphics. The R History I R is based on the computer language S, developed by John Chambers and others at Bell Laboratories in 1976. I In 1993 Robert Gentleman and Ross Ihaka at University of Auckland wanted to experiment with the langauge, so they developed an implementation, and named it R. I They made it open source in 1995. Its users are free to see how it is written and thousands of people around the world have contributed to its development. Design of the R System I The primary R system is available from the Comprehensive R Archive Network (CRAN) which hosts many add-on packages that can be used to extend the functionality of R. The R system is divided into 2 conceptual parts: I The ‘base’ R system that you download from CRAN: It contains the base package which is required to run R and contains the most fundamental functions. Other basic packages such as as stats, graphics, grid, cluster, et al. are included. To see which packages are loaded, run I Everything else: There are over 4000 add-on packages on CRAN that have been developed by users around the world and avaliable for download freely. Packages and Libraries In R, a package is a module containing functions, data, and documentation. To ensure a package is loaded, run code like library(knitr) To see which packages are loaded, run search() [1] ".GlobalEnv" "package:knitr" "package:stats" [4] "package:graphics" "package:grDevices" "package:utils" [7] "package:datasets" "package:methods" "Autoloads" [10] "package:base" To install a package from CRAN, you can run a command like install.packages("knitr") Difference between R and SAS I R is case-sensitive, while SAS is not in general. I For data management, SAS use row-by-row (observation-level) operation. But R is inherently a vector-based system. I For running massive data sets, R maybe a little slower than SAS. I R has no Graphical user interface (GUI) and no commercial hand-holding services. Introduction to the RStudio I RStudio is a cross-platform integrated development enviroment (IDE) for R. I R is the programming language. I RStudio may be viewed as a GUI for R. I RStudio allows us to create documents with embedded R code and output (R markdown). I RStudio facilitates use of R. I It integrates reproducible analysis with Sweave, knitr and R markdown as well as slide presentations, and includes a debugging enviroment. RStudio Panels I Source panel: This is the text editor for RStudio. I Console panel: This is the panel where you enter your R code and tell RStudio what to do to get the output. I Workspace panel: This panel shows what things RStudio has in its memory. I Other panel: This section includes files, plots, packages, help and viewer. Customizing RStudio RStudio options are accessible from the Options dialog Tools > Options menu (RStudio > Preferences on a Mac) and include the following categories: I General R Options I Source Code Editing I Appearance and Themes I Pane Layout I Packages I Sweave I Spelling I Git/SVN I Publishing First R Project I Create a new R Project for this class by clicking “File-> New Project -> New Directory-> New Project” I Enter the name ‘M3210Intro’. Try to avoid using blank spaces in the project name. Browse for the subdirectory of where you want to save your project (e.g. flashdrive, dropbox, onedrive, desktop) I Finally, clilck on ‘create project’, and a folder called ‘M3210Intro’ should now appear in iiyour selected subdirectory. I Type and execute getwd() in the R console to get your working directory. First R script Type the following into the text editor, and save this R script. # Name: Dr. Zeng # File Name: My first R script Example 9+6 #addition 9*6 #multiplication Next step, copy and paste this content to the console panel and then hit enter on your keyboard. Introduction to R markdown I R Markdown provides an authoring framework for data science. I You can use a single R Markdown file to save code, execute code, and generate high quality reports that can be shared with an audience I R Markdown supportsdynamic output formats including HTML, PDF, MS Word, Beamer, books, dashboards, scientific articles, websites, and more. I Your homework and activity assignments will be esubmitted as Rmarkdown files (.rmd) through emails. You are also required to turn in a hard copy for grading feedbacks. R markdown Tutorial I To open a R markdown file, go to “File-> New File-> R Markdown”. Select ‘document’ if you want to write a document or ‘presentation’ if your goal preparing slides. Typle the title and author name. Click OK. I R markdown Cheat Sheet I R markdown Reference Guide I R markdown template (Introduction to R markdown.rmd) I You can install the R Markdown package from CRAN with: install.packages("rmarkdown").