R Lesson : Variables in R | Dataplexa

Variables in R

In this lesson, you will learn what variables are and how they are used in R. Variables are one of the most important concepts in programming because they allow you to store and reuse data.

Once you understand variables, writing programs becomes much easier and more meaningful.


What Is a Variable?

A variable is a name given to a value stored in memory. Instead of remembering the value directly, you use the variable name to access it.

Variables help make programs readable, flexible, and easier to update.


Creating Variables in R

In R, variables are created using the assignment operator <-. This operator assigns a value to a variable.

The value is placed on the right side, and the variable name is on the left.

x <- 10
name <- "Dataplexa"

Here, the variable x stores a number and name stores text.


Rules for Naming Variables

R follows simple rules when naming variables. Following these rules prevents errors and confusion.

  • Variable names must start with a letter or a dot
  • They can contain letters, numbers, dots, and underscores
  • They cannot contain spaces
  • They are case-sensitive

Using meaningful names makes your code easier to understand.


Valid and Invalid Variable Names

Not all names are allowed in R. Let us see some correct and incorrect examples.

# Valid variable names
total_score <- 95
user_name <- "Alex"
.data_value <- 100

# Invalid variable names
# 2value <- 20
# user name <- "Sam"
# total-score <- 50

Invalid variable names cause errors and should always be avoided.


Assigning Different Types of Values

R allows variables to store different types of data. You do not need to specify the data type manually.

R automatically detects the type based on the value.

age <- 25          # numeric
price <- 99.5      # numeric
is_active <- TRUE  # logical
message <- "Hello" # character

This behavior makes R flexible and easy to use.


Reassigning Variable Values

In R, you can change the value of a variable at any time. The new value replaces the old one.

Only the most recent value is stored in memory.

x <- 5
x <- 20
x

After reassignment, the variable uses the updated value.


Using Variables in Calculations

Variables are often used in calculations. They allow you to reuse values instead of writing numbers repeatedly.

This reduces mistakes and improves clarity.

a <- 10
b <- 5

sum <- a + b
difference <- a - b
product <- a * b

R performs calculations using the stored variable values.


Printing Variable Values

You can display variable values using the print() function. This helps you verify results.

score <- 88
print(score)

Printed output appears in the console.


Variables and Case Sensitivity

R treats variable names with different letter cases as different variables.

This means value and Value are not the same.

value <- 10
Value <- 20

value
Value

Always use consistent naming to avoid confusion.


Best Practices for Variables

Good variable practices make your programs easier to read and maintain.

  • Use meaningful names
  • Avoid very short names except for simple cases
  • Use lowercase with underscores for readability
  • Keep naming style consistent

Clean code is easier to understand, even months later.


What’s Next?

Now that you understand variables, you are ready to learn about data types in R.

In the next lesson, you will explore different data types and how R handles them. This is an important step toward working with real data.