R Syntax

🧩 R Syntax

R syntax refers to the rules for writing R code. R is known for its simple and readable syntax, which makes it beginner-friendly.

 1. Basic R Statement

In R, a statement is written in one line and executed when you press Enter.



 2. Case Sensitivity

R is case-sensitive.


name and Name are different variables


 3. Assignment Operator (<-)

R uses <- to assign values to variables.


 You can also use =, but <- is recommended.


 4. Comments in R

Comments are used to explain code.
Anything after # is ignored by R.



 5. Print Output


Or simply type the variable name:

x

 6. Multiple Statements

You can write multiple statements in one line using ; (not recommended).


 Better practice: write each statement on a new line.


 7. Whitespace in R

Spaces do not affect R execution, but improve readability.



 8. Line Continuation

R automatically continues to the next line if code is incomplete.



 9. Keywords in R

Some words are reserved and cannot be used as variable names:

  • if, else

  • repeat, while, function

  • TRUE, FALSE, NULL

❌ Wrong:



 10. R Script File

R programs are saved with .R extension.

Example:


You can run the script in RStudio.


 Syntax Summary

Rule Description
Case-sensitive xX
Assignment <-
Comment #
End of line Enter key
File extension .R

You may also like...