The Wayback Machine - https://web.archive.org/web/20240713030301/https://www.geeksforgeeks.org/environments-in-r-programming/
Open In App

Environments in R Programming

Last Updated : 05 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The environment is a virtual space that is triggered when an interpreter of a programming language is launched. Simply, the environment is a collection of all the objects, variables, and functions. Or, Environment can be assumed as a top-level object that contains the set of names/variables associated with some values. In this article, let us discuss creating a new environment in R programming, list all environments, removing a variable from the environment, searching for a variable or function among environments and function environments with the help of examples.

Why the Environment Differ from the List?

  • Every object in an environment has a name.
  • The environment has a parent environment.
  • Environments follow reference semantics.

Create a New Environment

An environment in R programming can be created using new.env() function. Further, the variables can be accessed using $ or [[ ]] operator. But, each variable is stored in different memory locations. There are four special environments: globalenv(), baseenv(), emptyenv() and environment()

Syntax: new.env(hash = TRUE)

Parameters: 
hash: indicates logical value. If TRUE, environments uses a hash table

To know about more optional parameters, use below command in console: help(“new.env”) 

Example:

R




# R program to illustrate
# Environments in R
  
# Create new environment
newEnv <- new.env()
  
# Assigning variables
newEnv$x <- 1
newEnv$y <- "GFG"
newEnv$z <- 1:10
  
# Print
print(newEnv$z)


Output: 

 [1]  1  2  3  4  5  6  7  8  9 10

List all Environments

Every environment has a parent environment but there is an empty environment that does not have any parent environment. All the environments can be listed using ls() function and search() function. ls() function also list out all the bindings of the variables in a particular environment.  

Syntax: 

ls() 
search() 

Parameters:
These functions need no argument

Example: 

R




# R program to illustrate
# Environments in R
  
# Prints all the bindings and environments 
# attached to Global Environment
ls()
  
# Prints bindings of newEnv
ls(newEnv)
  
# Lists all the environments of the parent environment
search()


Output: 

[1] "al"     "e"      "e1"     "f"      "newEnv" "pts"    "x"      "y"     
[9] "z"

[1] "x" "y" "z"

[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base" 

Removing a Variable From an Environment

A variable in an environment is deleted using rm() function. It is different from deleting entries from lists as entries in lists are set as NULL to be deleted. But, using rm() function, bindings are removed from the environment. 

Syntax: rm(…) 

Parameters: 
…: indicates list of objects 

Example: 

R




# R program to illustrate
# Environments in R
  
# Remove newEnv
rm(newEnv)
  
# List
ls()


Output: 

[1] "al"  "e"   "e1"  "f"   "pts" "x"   "y"   "z"

Search a Variable or Function Among Environments

A variable or a function can be searched in R programming by using where() function among all the environments and packages present. where() function is present in pryr package. This function takes only two arguments, the name of the object to search for and the environment from where to start the search.

Syntax: where(name) 

Parameters: 
name: indicates object to look for 

Example: 

R




# R program to illustrate
# Environments in R
  
# Install pryr package
install.packages("pryr")
  
# Load the package
library(pryr)
  
# Search
where("x")
  
where("mode")


Output: 

<environment: R_GlobalEnv>
<environment: base>


Previous Article
Next Article

Similar Reads

Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
determinant() function in R Language is a generic function that returns separately the modulus of the determinant, optionally on the logarithm scale, and the sign of the determinant. Syntax: determinant(x, logarithm = TRUE, ...) Parameters: x: matrix logarithm: if TRUE (default) return the logarithm of the modulus of the determinant Example 1: # R
2 min read
tidyr Package in R Programming
Packages in the R language are a collection of R functions, compiled code, and sample data. They are stored under a directory called “library” in the R environment. By default, R installs a set of packages during installation. One of the most important packages in R is the tidyr package. The sole purpose of the tidyr package is to simplify the proc
14 min read
Get Exclusive Elements between Two Objects in R Programming - setdiff() Function
setdiff() function in R Programming Language is used to find the elements which are in the first Object but not in the second Object. Syntax: setdiff(x, y) Parameters: x and y: Objects with sequence of itemsR - setdiff() Function ExampleExample 1: Apply setdiff to Numeric Vectors in R Language C/C++ Code # R program to illustrate # the use of setdi
2 min read
Add Leading Zeros to the Elements of a Vector in R Programming - Using paste0() and sprintf() Function
paste0() and sprintf() functions in R Language can also be used to add leading zeros to each element of a vector passed to it as argument. Syntax: paste0("0", vec) or sprintf("%0d", vec)Parameters: paste0: It will add zeros to vector sprintf: To format a vector(adding zeros) vec: Original vector dataReturns: Vectors by addition of leading zeros Exa
1 min read
Clustering in R Programming
Clustering in R Programming Language is an unsupervised learning technique in which the data set is partitioned into several groups called clusters based on their similarity. Several clusters of data are produced after the segmentation of data. All the objects in a cluster share common characteristics. During data mining and analysis, clustering is
6 min read
Compute Variance and Standard Deviation of a value in R Programming - var() and sd() Function
var() function in R Language computes the sample variance of a vector. It is the measure of how much value is away from the mean value. Syntax: var(x) Parameters: x : numeric vector Example 1: Computing variance of a vector # R program to illustrate # variance of vector # Create example vector x &lt;- c(1, 2, 3, 4, 5, 6, 7) # Apply var function in
1 min read
Compute Density of the Distribution Function in R Programming - dunif() Function
dunif() function in R Language is used to provide the density of the distribution function. Syntax: dunif(x, min = 0, max = 1, log = FALSE) Parameters: x: represents vector min, max: represents lower and upper limits of the distribution log: represents logical value for probabilities Example 1: # Create vector of random deviation u &lt;- runif(20)
1 min read
Compute Randomly Drawn F Density in R Programming - rf() Function
rf() function in R Language is used to compute random density for F Distribution. Syntax: rf(N, df1, df2) Parameters: N: Sample Size df: Degree of Freedom Example 1: # R Program to compute random values # of F Density # Setting seed for # random number generation set.seed(1000) # Set sample size N &lt;- 20 # Calling rf() Function y &lt;- rf(N, df1
1 min read
Data Handling in R Programming
R Programming Language is used for statistics and data analytics purposes. Importing and exporting of data is often used in all these applications of R programming. R language has the ability to read different types of files such as comma-separated values (CSV) files, text files, excel sheets and files, SPSS files, SAS files, etc. R allows its user
5 min read
Return a Matrix with Lower Triangle as TRUE values in R Programming - lower.tri() Function
lower.tri() function in R Language is used to return a matrix of logical values with lower triangle as TRUE. Syntax: lower.tri(x, diag) Parameters: x: Matrix object diag: Boolean value to include diagonal Example 1: # R program to print the # lower triangle of a matrix # Creating a matrix mat &lt;- matrix(c(1:9), 3, 3, byrow = T) # Calling lower.tr
1 min read
Article Tags :
three90RightbarBannerImg