๐ Introduction
Syntax refers to the set of rules that define how programs are written in the R programming language. Understanding R syntax is the first step toward writing correct, readable, and efficient programs. Fortunately, R has a simple and intuitive syntax, making it an excellent language for beginners in data analysis, statistics, and machine learning.
Information
๐ Basic Structure of an R Program
An R program is a sequence of statements executed from top to bottom. Each statement performs a specific task such as creating variables, performing calculations, or displaying output.
Basic R Program
print("Welcome to R Programming")
name <- "Alice"
age <- 21
print(name)
print(age)๐ค Case Sensitivity
R is a case-sensitive language. This means that uppercase and lowercase letters are treated as different characters.
Case Sensitivity Example
value <- 100
Value <- 200
print(value)
print(Value)Warning
๐ฆ Variables and Assignment
Variables are used to store data. The preferred assignment operator in R is <-, although = can also be used in many situations.
Variable Assignment
city <- "London"
temperature <- 28
print(city)
print(temperature)๐ฌ Statements
A statement is an instruction executed by the R interpreter. Multiple statements can be written on separate lines or on the same line using a semicolon (;).
Separate Statements
x <- 10
y <- 20
print(x + y)Statements on One Line
x <- 10; y <- 20; print(x + y)๐จ Comments
Comments improve code readability and are ignored during program execution. Single-line comments begin with the # symbol.
Comments
# Store employee details
employee <- "David"
# Display employee name
print(employee)Tip
๐ค Displaying Output
The print() function is commonly used to display output in the R Console.
Printing Values
print("Hello")
print(150)
print(TRUE)๐ข Naming Rules for Variables
| Rule | Example |
|---|---|
| Can contain letters, numbers, underscores, and periods. | student_name |
| Must begin with a letter or a period not followed by a number. | score1 |
| Cannot begin with a number. | 1score โ |
| Avoid reserved keywords. | if โ |
๐ Reserved Keywords
Reserved keywords have predefined meanings in R and cannot be used as variable names.
- if
- else
- repeat
- while
- for
- function
- TRUE
- FALSE
- NULL
๐งฎ Expressions
An expression combines values, variables, and operators to produce a result.
Expression Example
a <- 15
b <- 5
result <- a * b + 20
print(result)๐ Code Blocks
Although R does not rely heavily on braces for every statement, multiple statements can be grouped using curly braces , especially inside functions, loops, and conditional statements.
Code Block
{
x <- 10
y <- 20
print(x + y)
}๐ Indentation
Proper indentation is not mandatory for execution, but it significantly improves code readability and maintenance.
Poor Formatting
if(TRUE){
print("Welcome")
print("Learning R")
}Good Formatting
if (TRUE) {
print("Welcome")
print("Learning R")
}๐ Execution Flow
โ ๏ธ Common Syntax Errors
| Error | Cause | Solution |
|---|---|---|
| Missing quotation marks | Text is not enclosed in quotes. | Use double or single quotation marks. |
| Incorrect capitalization | R is case-sensitive. | Match variable and function names exactly. |
| Using reserved keywords | Keyword used as a variable. | Choose another variable name. |
| Missing parenthesis | Function call is incomplete. | Ensure parentheses are balanced. |
๐ Real-World Example
Employee Details
employee_name <- "Sophia"
department <- "Finance"
salary <- 55000
print(employee_name)
print(department)
print(salary)๐ก Best Practices
- Use meaningful variable names.
- Follow consistent indentation.
- Keep one statement per line whenever possible.
- Write comments for complex logic.
- Use <- for variable assignment to follow standard R conventions.
Best Practice
๐ Summary
R syntax is designed to be simple, readable, and expressive. By understanding the rules for writing statements, declaring variables, using comments, following naming conventions, and maintaining proper formatting, you can create reliable and maintainable R programs. Mastering these syntax fundamentals provides a solid foundation for learning data structures, operators, functions, and advanced programming concepts in R.