R Variables

R Tutorial

R Variables

Variables in R are used to store data values so they can be reused and manipulated in a program.

 1. Creating Variables in R

R mainly uses the assignment operator <-.

  •  You can also use =, but <- is preferred.

 2. Rules for Variable Names

  •  Must start with a letter or dot (.)
  •  Can contain letters, numbers, underscore (_), dot (.)
  •  Cannot start with a number
  •  Cannot use R reserved keywords

 3. Case Sensitivity

R is case-sensitive.

  • marks and Marks are different variables

 4. Types of Values Stored in Variables

R automatically detects the data type.


 5. Printing Variable Values


 6. Multiple Variable Assignment


 7. Variable Reassignment

  •  Latest value replaces old value

 8. Removing Variables

Remove multiple variables:

Remove all variables:


 9. Check Variable Type


 10. Best Practices for Variables

  •  Use meaningful names (total_marks, student_age)
  •  Use lowercase with underscore
  •  Avoid single-letter variables (except loops)

 Summary

  • Variables store data

  • <- is preferred assignment operator

  • R is case-sensitive

  • Variables can be reassigned

You may also like...