Variables

R Variables – Creating, Naming and Using Variables in R

A variable is a memory space allocated to store specific data. The name associated with the variable is used to interact with this reserved memory block. The name given to a variable is known as its variable name. Typically, a variable stores data of a specific data type. The term “variable” reflects its nature—values can vary during the program’s execution.

Variables in R Programming

R is a dynamically typed language. This means variables in R are not explicitly declared with a data type but inherit the data type of the object assigned to them.

This characteristic is also seen in languages like Python and PHP.

Creating Variables in R

R supports three methods for assigning values to variables:

  1. Using the equal operator (=)
    Assigns values to variables directly.
  2. Using the leftward operator (<-)
    Copies data from right to left.
  3. Using the rightward operator (->)
    Copies data from left to right.

Syntax for Creating Variables

Equal Operator:

variable_name = value

Leftward Operator:

variable_name <- value

Rightward Operator:

value -> variable_name

Examples of Creating Variables in R

R Program to Illustrate Variable Creation

# Using equal operator
fruit = "apple"
print(fruit)

# Using leftward operator
city <- "Paris"
print(city)

# Using rightward operator
"blue" -> color
print(color)

Output:

[1] "apple"
[1] "Paris"
[1] "blue"
Nomenclature Rules for R Variables
  1. Variable names can include alphabets, numbers, dot (.), and underscore (_).
    Example: var_1 is valid.
  2. No special characters (except dot and underscore) are allowed.
    Example: var$1 or var#1 is invalid.
  3. Variable names must start with alphabets or a dot (.).
    Example: my_var or .myVar is valid.
  4. Variable names must not begin with numbers or underscores.
    Example: 1var or _var is invalid.
  5. If a variable starts with a dot, the character immediately following cannot be a number.
    Example: .3var is invalid.
  6. Reserved keywords in R (e.g., TRUEFALSE) cannot be used as variable names.
Important Functions for R Variables

1. class() Function: This function identifies the data type of a variable.

Syntax:

class(variable)

Example:

number = 42
print(class(number))

Output:

[1] "numeric"

2. ls() Function: Lists all the variables in the current R workspace.

Syntax:

ls()

Example:

# Assigning values to variables
name = "Alice"
age <- 30
height -> 5.5

print(ls())

Output:

[1] "age" "height" "name"

3. rm() Function: Deletes a specified variable from the workspace.

Syntax:

rm(variable)

Example:

# Assigning and removing a variable
country = "India"
rm(country)
print(country)

Output:

Error in print(country): object 'country' not found
Scope of Variables in R

1. Global Variables: Accessible throughout the program and declared outside of any block or function.

Example:

# Declaring and accessing global variables
global_var = 10

display = function() {
  print(global_var)
}

display()

# Modifying the global variable
global_var = 20
display()

Output:

[1] 10
[1] 20

2. Local Variables: Accessible only within the block or function where they are declared.

Example:

# Declaring and using a local variable
my_function = function() {
  local_var = "This is local"
  print(local_var)
}

cat("Inside the function:\n")
my_function()

cat("Outside the function:\n")
print(local_var)

Output:

Inside the function:
[1] "This is local"
Outside the function:
Error in print(local_var): object 'local_var' not found

Difference Between Local and Global Variables in R

AspectGlobal VariablesLocal Variables
ScopeAccessible throughout the program.Accessible only within the function/block.
LifetimeExist until the program finishes execution or is removed explicitly.Exist only during the function/block execution.
Naming ConflictsProne to conflicts as accessible globally.Limited scope minimizes conflicts.
Memory UsageOccupies memory throughout the program’s execution.Utilized temporarily, thus conserving memory.

Scope of Variable in R

Variables in R are containers used to store data values. They serve as references, or pointers, to objects in memory. When a variable is assigned a value, it points to the memory location where the value is stored. Variables in R can hold vectors, collections of vectors, or combinations of various R objects.

Example:

# Assigning a value using the equal sign
num_values = c(4, 5, 6, 7)
print(num_values)

# Assigning values using the leftward arrow
languages <- c("JavaScript", "R")
print(languages)

# Assigning a vector
numbers = c(10, 20, 30)
print(numbers)

names = c("Alice", "Bob", "Carol")
print(names)

# Creating a list of vectors
combined_list = list(numbers, names)
print(combined_list)

Output:

[1] 4 5 6 7
[1] "JavaScript" "R"
[1] 10 20 30
[1] "Alice" "Bob" "Carol"
[[1]]
[1] 10 20 30

[[2]]
[1] "Alice" "Bob" "Carol"
Naming Conventions for Variables

Variable names in R must follow these rules:

  • They must start with an alphabet.
  • Only alphanumeric characters, underscores (_), and periods (.) are allowed.
  • Special characters such as !@#$ are not allowed.

Example:

# Valid variable names
value1 = 15
student_name = "John"
course.Name = "Data Science"

# Invalid variable names
# 1value = 20
# student@name = "John"

Error:

Error: unexpected symbol in "1value"
Error: unexpected symbol in "student@name"
Scope of Variables

Global Variables: Global variables are accessible throughout the program. They are usually declared outside all functions and remain in memory until the program ends.

Example:

# Defining a global variable
global_value = 42

# Function accessing the global variable
show_value = function() {
  print(global_value)
}

show_value()

# Updating the global variable
global_value = 100
show_value()

Output:

[1] 42
[1] 100

Local Variables: Local variables exist only within the scope of the function or block in which they are defined. They are not accessible outside that block.

Example:

my_function = function() {
  # Defining a local variable
  local_value = 25
}

# Attempting to access the local variable
print(local_value)

Error:

Error in print(local_value) : object 'local_value' not found

To access the value, use print() within the function:

my_function = function() {
  local_value = 25
  print(local_value)
}

my_function()

Output:

[1] 25

Accessing Global Variables: Global variables are accessible across the program, but local variables are restricted to their scope.

Example:

set_value = function() {
  # Local variable
  local_var <- 15
}

set_value()
print(local_var)

Error:

Error in print(local_var) : object 'local_var' not found

To create or modify global variables inside a function, use the super assignment operator (<<-):

set_global = function() {
  # Making a global variable
  global_var <<- 50
  print(global_var)
}

set_global()
print(global_var)

Output:

[1] 50
[1] 50

Dynamic Scoping in R Programming

R is an open-source programming language extensively used for statistical analysis and data visualization. It operates through a command-line interface and is compatible with popular platforms like Windows, Linux, and macOS. As a cutting-edge programming tool, R employs lexical scoping or static scoping as its primary mechanism for resolving free variables, distinguishing it from other languages that may use dynamic scoping.

Dynamic Scoping: The Concept

Consider the following function:

h <- function(a, b) {
  a^2 + b/c
}

Here, the function h has two formal arguments a and b. Inside the function body, there is another symbol c, which is a free variable. Scoping rules determine how free variables like c are resolved. Free variables are neither formal arguments nor local variables assigned within the function.

Lexical Scoping in R
Under lexical scoping, the value of a free variable is resolved in the environment where the function was defined. If the variable’s value isn’t found in the defining environment, the search continues in its parent environment until the global environment or namespace is reached. If the variable remains undefined even in the global environment, an error is raised.

Illustrative Example of Lexical Scoping
create.power <- function(m) {
  power.calc <- function(num) {
    num ^ m
  }
  power.calc
}

cube <- create.power(3)
square <- create.power(2)

print(cube(4))   # Output: [1] 64
print(square(5)) # Output: [1] 25

Here, the function create.power generates a closure, where m is a free variable inside the nested power.calc function. The value of m is determined from the environment where create.power was defined.

Inspecting Function Environments
ls(environment(cube))
# Output: [1] "m" "power.calc"

get("m", environment(cube))
# Output: [1] 3

ls(environment(square))
# Output: [1] "m" "power.calc"

get("m", environment(square))
# Output: [1] 2

The environment() function retrieves the environment of a function, while ls() lists the variables in that environment. The get() function fetches the value of a specific variable within the environment.

Comparing Lexical and Dynamic Scoping

Let’s take another example to illustrate the difference:

z <- 5

f <- function(x) {
  z <- 3
  y <- function(p) {
    p + z
  }
  y(x)
}

y <- function(p) {
  p * z
}

Lexical Scoping: In this scenario, z in y(x) will resolve to 3, as it is found in the local environment of f.
Dynamic Scoping: If dynamic scoping were used, z would resolve to 5, as it would be looked up in the calling environment.

Output with Lexical Scoping:

print(f(2))  # Output: [1] 5

Example Demonstrating Errors with Free Variables

y <- 4

g <- function(x) {
  b <- 2
  x + b + y
}

print(g(3))  # Output: [1] 9

If y is not defined in the environment, calling g(3) results in the following error:

Error in g(3): object 'y' not found

To fix this, ensure y has a value assigned before calling the function.

Lexical Scoping in R Programming

Lexical Scoping in R programming means that the values of the free variables are searched for in the environment in which the function was defined. An environment is a collection of symbols, value, and pair, every environment has a parent environment it is possible for an environment to have multiple children but the only environment without the parent is the empty environment. If the value of the symbol is not found in the environment in which the function was defined then the search is continued in the parent environment. In R, the free variable bindings are resolve by first looking in the environment in which the function was created. This is called Lexical Scoping.

Why Lexical Scoping?

Lexical scoping determines how R identifies the value of a variable based on the function’s definition rather than its execution context. This built-in behavior simplifies understanding variable resolution in R. Lexical scoping allows users to focus solely on the function’s definition, rather than its invocation, to predict how variables are resolved. This concept is especially beneficial in statistical computations.

The term “lexical” in lexical scoping originates from the concept of “lexing” in computer science, which is the process of breaking down code into meaningful units that can be interpreted by a programming language.

Example:

f <- function(a, b) {
  a + b + c
}

c <- 5
f(2, 3)

Output:

[1] 10

In this case:

  • a and b are formal arguments.
  • c is a free variable. Its value is retrieved from the environment where the function was defined.
Principles of Lexical Scoping

There are four fundamental principles of lexical scoping in R:

  1. Name Masking
  2. Functions vs Variables
  3. A Fresh Start
  4. Dynamic Lookup

1. Name Masking: When a variable’s name is not defined within a function, R looks for the variable in the parent environment.

Example:

x <- 15
y <- function(a, b) {
  a + b + x
}

y(5, 10)

Output:

[1] 30

Explanation: The function adds 5, 10, and the value of x from the global environment.

If a variable is defined inside the function, it masks the variable from the parent environment.

Example:

x <- 20
y <- function() {
  x <- 5
  x + 10
}

y()

Output:

[1] 15

Explanation: The local definition of x within the function takes precedence.

2. Functions vs Variables: R applies the same scoping rules to functions and variables.

Example:

f <- function(x) x * 2
g <- function() {
  f <- function(x) x + 5
  f(3)
}

g()

Output:

[1] 8

3. A Fresh Start: Every time a function is called, R creates a new environment, ensuring that the function’s execution is independent of previous calls.

Example:

a <- function() {
  if (!exists("y")) {
    y <- 10
  } else {
    y <- y + 10
  }
  y
}

a()

Output:

[1] 10

4. Dynamic Lookup: Lexical scoping resolves where to look for variable values, but the lookup occurs at the time of function execution.

Example:

h <- function() z + 5
z <- 10
h()

Output:

[1] 15
Identifying Global Variables

The findGlobals() function from the codetools package identifies global variables used within a function. This can help in understanding external dependencies.

Example:

xGlobal <- runif(5)
yGlobal <- runif(5)

f <- function() {
  x <- xGlobal
  y <- yGlobal
  plot(y ~ x)
}

codetools::findGlobals(f)

Output:

[1] "{"       "~"       "<-"      "xGlobal" "yGlobal" "plot"

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *