The assign() function creates variables programmatically;useful when you don’t know the variable name until runtime. Most of the time, you’d just use my_var <- value, but assign() is invaluable when you’re generating variable names dynamically, working with environments, or writing functions that need to create variables.
I’ll show you exactly when and how to use this function, plus common pitfalls to avoid.
When to Use assign()
You’ll typically use assign() when:
- Creating multiple variables in a loop
- Generating variable names based on data
- Writing functions that create variables
- Working with different environments programmatically
- Meta-programming or dynamic variable creation
Basic Syntax
assign(x = "variable_name", value = data, envir = .GlobalEnv)
- x: Variable name as a string (e.g., “my_var”)
- value: What you want to assign
- envir: Environment where variable is created (usually .GlobalEnv)
Examples with Explanations
Use assign() Function to Single Variable
Let’s see how we can use assign value to variable using assign() function:
# Assign value to variable
assign("var1",52)
# Show variable
print(var1)
Output:
[1] 52
The output shows value 52 assign to var1 variable.
Use assign() Function to Vector
Using assign() function you can assign values to vector:
# Assign vector of values to variable
assign("vect1",c(12,14,45,85,"A","u"))
# Print variable
print(vect1)
Output:
[1] "12" "14" "45" "85" "A" "u"
Here the output shows values assign to vect1 variable.
Use assign() Function to Multiple Variable
You can use assign() function to assign values to multiple variables:
# Use for loop to assign values to several variables
for(i in 1:5){
assign(paste0("var",i),i)
}
# Show variable
print(var1)
print(var2)
print(var3)
print(var4)
print(var5)
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
As we can see using assign() function with for loop we assign values to five variables.
Common Mistakes to Avoid
Mistake 1: Forgetting the quotes around variable names
# ❌ WRONG - This creates a variable named "x", not what you intended
assign(x, 10) # Error: object 'x' not found
# ✅ CORRECT - The variable name must be a string
assign("x", 10)
print(x) # [1] 10
Mistake 2: Not understanding variable scope
# ❌ WRONG - Variable created in function environment, not global
my_function <- function() {
assign("result", 42) # Creates in function environment by default
}
my_function()
print(result) # Error: object 'result' not found
# ✅ CORRECT - Specify .GlobalEnv to create in global scope
my_function <- function() {
assign("result", 42, envir = .GlobalEnv)
}
my_function()
print(result) # [1] 42
Mistake 3: Creating unintended variable overwriting
# ⚠️ BE CAREFUL - assign() can overwrite existing variables
x <- 100
for (i in 1:3) {
assign("x", i) # Overwrites x each time!
}
print(x) # [1] 3 (lost the original 100)
# ✅ BETTER - Use unique names
for (i in 1:3) {
assign(paste0("x_", i), i)
}
print(x_1) # [1] 1
print(x_2) # [1] 2
Pro Tips
- Prefer direct assignment - Use
<-when you know variable names at write time - Use assign() for dynamic names - Only use when name generation is necessary
- Be explicit with environments - Always specify
envirto avoid confusion - Document your code - Dynamic variable creation can be confusing for others (and future you!)