VOOZH about

URL: https://www.geeksforgeeks.org/r-language/basic-syntax-in-r-programming/

⇱ Basic Syntax in R Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Basic Syntax in R Programming

Last Updated : 20 Feb, 2026

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:


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:

  1. = (Simple Assignment)
  2. <- (Leftward Assignment)
  3. -> (Rightward Assignment)

Example:


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:


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.

👁 reserved_words_in_r
Reserved words in R
  • 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 ;


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.


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.

7. Printing Output

R provides different functions to display output.


Output
[1] "Using print function"
Using cat function
  • print() displays output with index numbers.
  • cat() prints output in a cleaner format.
Comment
Article Tags:
Article Tags:

Explore