The get Function in R (5 Examples)
In this article, I’ll show you how to use the get function in R. Let’s first have a look at the basic R syntax and the definition of the get function:
Basic R Syntax:
The get function searches and calls a data object.
In the following tutorial, I’m going to show you two examples for the usage of the get function as well as three alternative functions (i.e. get0, mget, and exists).
So without further ado, let’s dive into it!
Example 1: Apply get R Function to a Vector
In this example, I’m going to apply the get command to a vector. So let’s create an example vector first:
x1 <- c(3, 1, 1, 7, 9) # Create example vector
If we want to call (i.e. return) this vector to the RStudio console, we can use the get function as follows:
get("x1") # Apply get function # 3 1 1 7 9
If you need more explanations on this example, you may check out the following YouTube video. In the video, I’m explaining Example 1 in some more detail.
Example 2: Get Column of a Data Frame
The get function can also be used to call a column from a data frame. Let’s first create some example data:
data <- data.frame(var1 = c(5, 5, 5, 5, 5), # Create example data.frame var2 = c(4, 2, 2, 1, 8))
In order to use the get function for the variables of this data frame, we first need to attach the data:
attach(data) # Attach example data
Now, we can apply the get function as we did in Example 1:
get("var1") # Apply get to column of attached data # 5 5 5 5 5
Note: Don’t forget to detach the data frame after applying get. Otherwise, the rest of your R code might get flawed due to the attachment.
detach(data) # Detach example data
Alternatives to the get R Function
The R programming language provides several functions, which are similar but not identical to the get function. In the following, I’ll show you three of the most commonly used alternatives to the get function.
Alternative 1: The get0 Function
The get0 function works as the get function, but with the additional option to specify what should happen in case the data object you are searching for is not existent.
Let’s apply get0 to the x1 vector that we have created in Example 1:
get0("x1", ifnotfound = "not available") # Apply get0 function # 3 1 1 7 9
As you can see based on the R code above, we specified the option ifnotfound = “not available”, i.e. in case x1 would not be available, get0 would return the character string “not available” to the RStudio console.
Let’s try this in practice:
If we apply the get0 function to the data object y, which does not exist in our R environment, the get0 function returns “not available:
get0("y", ifnotfound = "not available") # Apply get0 to not available object # "not available"
Alternative 2: The mget Function
Another alternative to get() is the mget function. In contrast to the get command, mget can be applied to several data objects at the same time.
Let’s first create some further example vectors:
x2 <- c(1, 9, 1) # Create second example vector (numeric) x3 <- c("A", "hello", "mmm") # Create third example vector (string)
The vector x2 is another numeric vector (as x1) and the vector x3 is a character vector.
Now let’s apply mget to our three example vectors x1, x2, and x3:
mget(c("x1", "x2", "x3")) # Apply mget function
Figure 1: RStudio Output List Created by the mget Function.
As you can see in Figure 1, mget returns a list containing our three example vectors.
Alternative 3: The exists Function
The exists function does only check for the existence of data objects, but it does not return the data object (as the get function does). Let’s try that in an example:
exists("x1") # Apply exists to existent object # TRUE
If we apply exists to the x1 vector, the function returns the logical value TRUE.
However, if we apply the exists function to y (i.e. a data object that is not stored in our global R environment), the exists function returns FALSE:
exists("y") # Apply exists to non-existent object # FALSE
Since the exists function is often used among R users, I have written an R tutorial which is dedicated exclusively to the exists function. If you are interested, you can check out the tutorial HERE.
Video Examples of the get Function
In case you need more examples for the get function, you might want to have a look at the following video of the YouTuber Yi Li. In the video, he is showing you further examples for the application of get in R.