Lesson 1: Getting Started: Basic RLesson 1: Getting Started: Basic R
Our main goal here is to become familiar with basic operations in R. We'll be using Chapter 1 of Essential R.
- enter a small vector of data using the function
- retrieve mean, standard deviation, and other summary data
- use the indexing operator, , to access parts of a data vector
- retrieve previous commands using the arrow keys
R Data and R Code Files
The R code file and data files for this lesson can be found on the Essential R - Notes on learning R page.
1.1 - Introduction to R Studio1.1 - Introduction to R Studio
I do recommend that you use RStudio if you can, at least to get started - it makes it easier to work in R. Here I demonstrate some of the useful features of R studio:
- File browser and file chooser
- Viewing of data objects
- Import data from text file wizard
- Auto-complete of objecsts in workspacetab to autocomplete or for function hints in the console
Newer (and more advanced RStudio features will be demo'd later). Also note:
- there are a host of keyboard shortcuts in RStudio and that the assignment operator
<-can be inserted using alt + -.
- you may want to change a couple of options in RStudio to make the editor easier to use: Go to Tools>Options>Code Editing, and then select "Soft-wrap R source files", and "Highlight selected line".
- Most of the videos that follow will feature only the R console or the R console + the Plot pane. This is done only to limit the filesize for the videos. It is best practice to write your code in the editor (R script) pane, and submit it to the console usingCtrl + Enter (Mac: Cmd + Enter). This is because the console is not saved - the script file is saved. Not saving the console may seem like a limitation, but in a few chapters, we'll introduce tools that make this limitation irrelevant.
1.2 - Introduction to the Console - Part 11.2 - Introduction to the Console - Part 1
Here we'll demonstrate basic use of the console in R, and demonstrate basic mathematical operations and the creation of variables.
1.3 - Introduction to the Console - Part 21.3 - Introduction to the Console - Part 2
Here we demonstrate how mathematical operations can be carried out with variables, how variables can be changed, and how variables can be removed.
1.4 - Creating Vectors1.4 - Creating Vectors
In this walkthrough we'll learn how to create vectors (data) using the function
c(), and see how the operations we learned in the last video apply to vectors.
1.5 - Indexing Vectors & Logical Extraction1.5 - Indexing Vectors & Logical Extraction
Here we introduce the indexing operator
[ ], which is so powerful (and sometimes a bit mysterious) that I refer to it as "the magical [ ]". We'll learn how to use logical test with the
[ ] to perform "logical extraction".
1.6 - Operations with Vectors1.6 - Operations with Vectors
Here we'll examine how vectors can interact with other vectors, either via mathematical operations or via indexing.
1.7 - Basic Loops in R1.7 - Basic Loops in R
Since R is a programming language, it is simple to write loops in R, as we'll see here. As we'll see throughout the course, because R natively handles vectors, it is often not necessary to write loops in R.
1.8 - Schematic of R Work-flow1.8 - Schematic of R Work-flow
Console, workspace, scriptfile, history....it all sounds confusing at first. This diagram might help clear thing up:
The console is where you interact with R - where commands are entered and results displayed.The workspace is where all the things R has in memory are stored. From the console the user creates and interacts with objects in the workspace. These interactions can be automated using scripts (these are just text files saved with a .R file extension rather than the standard .txt). An entire script can be run automatically (this is called "sourcing", and is done using the command "source()"). Or selected lines from a script can be run. The history records commands from the console - they can be retrieved using the up and down arrow keys (some R interfaces also have a history browser). Using the history can save a lot of typing. External data files can be read to and written from the workspace by entering commands in the console.