Basic R Syntax
Learn the foundational skills for writing an R script from scratch, such as syntax, variables, and arithmetic operations.
We'll cover the following...
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:
- Take in some data
- Manipulate it and perform some statistical operations
- 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.
#Take some input and assign it to a variableVAR_InputText <- "Hello, world!"#Print the variable’s contentsVAR_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 ...