# 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:**

get("x") |

get("x")

**Definition:**

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

The get R function is typically applied to data objects such as vectors, data.frames, or lists.

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 |

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 |

get("x1") # Apply get function # 3 1 1 7 9

## 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)) |

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 |

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 |

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 |

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 |

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" |

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) |

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 |

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 |

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 |

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.

## Further Reading

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R