What is the assign() function in R?
Overview
The assign() function in R is simply used to assign a value to a name in an environment.
Syntax
assign(x, value, pos = -1, envir = as.environment(pos),
inherits = FALSE, immediate = TRUE)
Required parameter values
The assign() function takes the following mandatory parameter values:
x: This represents the variable name that is given as a character string.value: This is the value to be assigned to thexvariable.
Optional parameter values
The assign() function takes the following optional parameter values:
pos: This represents where to do the assignment. By default, it is done in the current environment.envir: This represents the environment.inherits: This is used in case the enclosing frames of the environment need to be inspected or not.immediate: This is an ignored compatibility feature.
Code example
# using the assign() functionassign('x', 'Theo')x# using the assign() functionassign(x, 42)Theo
Code explanation
- Line 2: We use the
assign()function to assign the value"Theo"to the variable namex. - Line 6: We use the
assign()function to assign the value42to the variablex, which is already the same asTheoin the environment. - Line 7: We print the variable
Theo.