R Syntax

๐Ÿ“˜ 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

Unlike many programming languages, R allows you to write concise code while providing powerful built-in functions for data analysis.

๐Ÿ“ 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

value and Value are two different variables in R.

๐Ÿ“ฆ 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

Write comments to explain the purpose of the code instead of describing every line.

๐Ÿ“ค 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

RuleExample
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

โœ๏ธ Write R Code
Save the Script
Execute the Script
Interpreter Reads Each Statement
Statements Execute Sequentially
Results Display in Console

โš ๏ธ Common Syntax Errors

ErrorCauseSolution
Missing quotation marksText is not enclosed in quotes.Use double or single quotation marks.
Incorrect capitalizationR is case-sensitive.Match variable and function names exactly.
Using reserved keywordsKeyword used as a variable.Choose another variable name.
Missing parenthesisFunction 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

Clean and well-formatted code is easier to read, debug, and maintain, especially when working on large projects or collaborating with others.

๐Ÿ“ 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.

>>"Good syntax is not just about writing code that worksโ€”it is about writing code that people can easily understand and maintain."