R Variables

Used to store and manipulate data. Each variable can be decalred with a data type that decribes the values it can hold. You can assign a value to a variable using <- and =


# Using <-
x <- 10

# Using =
y = 5

print(x + y)

Rules for Declaring Variable Names

To declare a variable name correctely here are some rules




Valid Characters

Letters (uppercase and lowercase), digits, underscore

my_variable <- 10


Any length, but only the first 31 characters matter

averageScore, temperature2

Reserved Keywords

Cannot be the same as C keywords

if <- 5 (Invalid)

Case Sensitivity

C is case-sensitive

myVar and myvar are different

Meaningful and Descriptive

Names should convey the purpose of the variable (Not important but recommended)

totalStudents, averageTemperature

Avoid Starting with a Dot

While dots are allowed, avoid starting variable names with a dot.

.variable_name <- 50
(Avoid starting with a dot)

No Spaces

Variable names cannot contain spaces

student_age, total Score (Invalid)

See the example to get a clear view of how the rules works.


# Rule: Start with a Letter
my_variable <- 10

# Rule: Letters, Numbers, Dots, and Underscores
my_variable2 <- 20
user.name <- "John_Doe"

# Rule: Case-Sensitive
myVariable <- 30
myvariable <- 40

# Rule: Reserved Keywords (Invalid Example)
# Uncommenting the line below will result in an error.
# if <- 5

# Rule: Length (Choose meaningful names)
average_height <- mean(heights)
# Avoid excessively long names
# average_height_calculated_from_given_heights <- mean(heights)

# Rule: Avoid Starting with a Dot
.variable_name <- 50
# Avoid starting with a dot
# .invalid_variable <- 60

# Rule: Reserved Variables (Invalid Example)
# Uncommenting the line below will result in an error.
# mean <- 25

Data Type for a variable

We decide a data type for a variable by assign the value
If we assign a value to a variable then it is a Numerical data type
Similarly, we assign a value in " " is a Character data type
if we assign TRUE or Fasle then it's a Logical data type


# Numeric
num_var <- 3.14

# Character
char_var <- "Hello, R!"

# Logical
logical_var <- TRUE

Get to know more about data types in R

Quick Recap - Topics Covered

R Variables

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.

Example js

Example 1 Example 2 Example 3 Example 4 Example 5