R has a simple and readable syntax that makes it easy to start programming. Understanding the basic structure of R code helps you write clean and correct programs. R executes commands line by line and provides immediate output in the console.
- R is case-sensitive (x and X are different).
- Each statement is executed separately.
- Comments are written using #.
Writing and Running Code
R code can be written in:
- R Console
- RStudio Script
- Jupyter Notebook
Example:
print("Hello, World!")
Output
[1] "Hello, World!"
Syntax of an R Program
An R program mainly consists of:
- Variables
- Comments
- Keywords
These components form the basic structure of any R script.
1. Variables in R
Variables are used to store values so they can be reused later in the program. In R, assignment can be done in three ways:
- = (Simple Assignment)
- <- (Leftward Assignment)
- -> (Rightward Assignment)
Example:
var1 = "Simple Assignment"
var2 <- "Leftward Assignment!"
"Rightward Assignment" -> var3
print(var1)
print(var2)
print(var3)
Output
[1] "Simple Assignment" [1] "Leftward Assignment!" [1] "Rightward Assignment"
The rightward assignment is less common and can be confusing for some programmers, so it is generally recommended to use the <- or = operator for assigning values in R.
2. Comments in R
Comments are used to explain code and improve readability. They are ignored by the R interpreter during execution.
Example:
# This is a single line comment
print("This is fun!")
if(FALSE)
{
"This is multi-line comment which should be put inside either a single or a double quote"
}
Output
[1] "This is fun!"
From the above output, we can see that both comments were ignored by the interpreter.
3. Keywords in R
Keywords are reserved words in R that have special meaning. They cannot be used as variable names or function names.

- if, else, repeat, while, function, for, in, next and break are used for control-flow statements and declaring user-defined functions.
- The ones left are used as constants like TRUE/FALSE are used as boolean constants.
- NaN defines Not a Number value and NULL are used to define an Undefined value.
- Inf is used for Infinity values.
4. Semicolons and Multiple Statements
You can write multiple statements in one line using a semicolon ;
x <- 5; y <- 6; print(x + y)
Output
[1] 11
However, writing each statement on a new line improves readability.
5. Braces and Code Blocks
Curly braces {} are used to group multiple statements together, especially in control structures and functions.
if (TRUE) {
print("This is a block of code")
print("Multiple lines inside braces")
}
Output
[1] "This is a block of code" [1] "Multiple lines inside braces"
6. Object Naming Rules
- Names can contain letters, numbers, . and _.
- Names cannot start with a number.
- Avoid using reserved words like if, else, function.
my_value <- 100
value2 <- 50
7. Printing Output
R provides different functions to display output.
print("Using print function")
cat("Using cat function\n")
Output
[1] "Using print function" Using cat function
- print() displays output with index numbers.
- cat() prints output in a cleaner format.