📦 R Variables – How to Declare, Assign, and Use Variables in R
🧲 Introduction – What Are Variables in R?
In R, variables are used to store data—such as numbers, text, or logical values—that can be reused throughout your program. Unlike many other languages, R is dynamically typed, meaning you don’t have to declare a variable’s type before assigning a value.
Whether you’re storing a dataset, calculating a result, or labeling a string, variables are at the heart of R programming.
🎯 In this guide, you’ll learn:
- How to create and assign variables in R
- Different assignment operators (
<-
,=
,->
) - Naming conventions and rules
- How R handles data types in variables
✍️ How to Create a Variable in R
You can create variables using either <-
, =
, or ->
.
✅ Examples:
x <- 10 # Preferred assignment operator
y = 20 # Alternative, valid syntax
30 -> z # Rightward assignment
You can now use these variables in expressions:
sum <- x + y + z
print(sum)
🧾 Output:
[1] 60
📛 R Variable Naming Rules
Variable names (also called identifiers) must follow certain rules:
Rule | Example |
---|---|
Must start with a letter or . (not followed by a number) | total , .value |
Can contain letters, numbers, dots (. ), and underscores (_ ) | data_2025 , temp.value |
Case-sensitive | Score ≠ score |
Cannot be a reserved keyword | ❌ if , TRUE , function , else |
❌ Invalid Examples:
1value <- 10 # Cannot start with a number
TRUE <- 5 # Cannot override reserved constants
_myvar <- 50 # Underscore at start is not allowed
🔄 Assignment Operators in R
R provides three assignment operators:
Operator | Usage | Direction |
---|---|---|
<- | x <- 5 | Assigns right value to left (most common) |
= | x = 5 | Same as <- in most cases (used in function calls) |
-> | 5 -> x | Assigns left value to right |
⚠️ Recommendation:
Use <-
as the standard in scripts and functions, as it’s more idiomatic in R programming.
🔤 Variable Types (Implicit Typing)
R automatically assigns the correct data type based on the value.
name <- "Alice" # character
score <- 85 # numeric
is_passed <- TRUE # logical
To check the type of a variable:
class(name) # Output: "character"
typeof(score) # Output: "double"
is.logical(is_passed) # Output: TRUE
📂 Remove or List Variables
To delete a variable:
rm(score) # Removes 'score'
To view all active variables in your workspace:
ls() # Lists variable names
📌 Summary – Recap & Next Steps
Variables are the building blocks of R programming. Knowing how to define, use, and manage them allows you to write clean, efficient, and scalable R code.
🔍 Key Takeaways:
- Use
<-
for assigning values (e.g.,x <- 10
) - R is case-sensitive (
var
≠Var
) - Variable names must not start with numbers or use reserved keywords
- Use
rm()
to remove variables andls()
to view them - R automatically assigns data types to variables
⚙️ Real-World Relevance:
Variables allow data scientists to store values, manage datasets, create transformations, and build machine learning models efficiently in real-world R workflows.
❓ FAQs – R Variables
❓ What is the best assignment operator in R?
✅ <-
is preferred and most widely used in R for clarity and consistency.
❓ Are R variables case-sensitive?
✅ Yes. age
, Age
, and AGE
are treated as three separate variables.
❓ How can I check the type of a variable in R?
✅ Use class()
or typeof()
functions:
class(x) # "numeric", "character", etc.
❓ Can I reassign a variable with a different data type?
✅ Yes. R allows reassignment:
x <- 10
x <- "Now I am text"
❓ How do I remove all variables at once?
✅ Use:
rm(list = ls())
Share Now :