Basic R Syntax

Learn the foundational skills for writing an R script from scratch, such as syntax, variables, and arithmetic operations.

R has six main components: base-R, the R console, CRAN, an IDE, scripts, and the working directory. This lesson will focus on base-R, the R console, scripts, and the IDE. We won’t be using any extensions from CRAN yet, and we won’t be setting up a working directory—these will be discussed in more detail in the upcoming lessons.

Code structure

Any R script in this course will follow the same basic structure:

  1. Take in some data
  2. Manipulate it and perform some statistical operations
  3. Output the results in one form or another

Structuring our code with these three major sections is the best practice that delineates data science code in R. Over time, we’ll add complications and sub-components, but this will always be the primary underlying structure.

Our first example

That being said, we’ll start with a typical “Hello, world!” example and build toward a data-driven model throughout this lesson.

Press + to interact
#Take some input and assign it to a variable
VAR_InputText <- "Hello, world!"
#Print the variable’s contents
VAR_InputText

Value assignment

Line 1 is an assignment operation. It puts the value “Hello, world!” into the variable VAR_InputText. Notice that there are no declared data types, as there would be in some other programming languages. In R, it’s not necessary to specify what kind of data a variable will contain.

Also notice the operator <-, which is one ...