R Syntax

R Tutorial

 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 writting in one line and execute 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 using 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 reserving and cannot be use 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

RuleDescription
Case-sensitive xX
Assignment<-
Comment#
End of lineEnter key
File extension.R

You may also like...