Extract Just Number from Named Numeric Vector in R (3 Examples)

 

In this tutorial, I’ll illustrate how to remove the names from a named vector or array in the R programming language.

Table of contents:

So now the part you have been waiting for – the tutorial:

 

Constructing Example Data

We’ll use the following data as basement for this R tutorial:

x <- 1:5                       # Create named vector
names(x) <- letters[1:5]
x                              # Print named vector
# a b c d e 
# 1 2 3 4 5

As you can see based on the previous output of the RStudio console, our example data object is a named vector with five elements.

 

Example 1: Remove Names from Numeric Vector Using unname Function

The following syntax shows how to extract only the numeric values from our vector.

x_new1 <- unname(x)            # Apply unname
x_new1                         # Return updated vector
# 1 2 3 4 5

As you can see based on the previous output, we stored the numeric values without their names in the new data object x_new1.

 

Example 2: Remove Names from Numeric Vector Using as.numeric Function

The following R programming code illustrates how to delete names from a numeric vector object using the as.numeric function in R.

x_new2 <- as.numeric(x)        # Apply as.numeric
x_new2                         # Return updated vector
# 1 2 3 4 5

The output is the same as in Example 1.

 

Example 3: Remove Names from Numeric Vector Using names Function

The following R code shows how to extract just the numbers from a named vector by applying the names command.

x_new3 <- x                    # Replicate vector
names(x_new3) <- NULL          # Apply names
x_new3                         # Return updated vector
# 1 2 3 4 5

Again, the output is the same as in the previous examples.

 

Video, Further Resources & Summary

Do you want to learn more about the handling of named data objects? Then you may watch the following video that I have published on my YouTube channel. I’m explaining the R codes of this page in the video.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might want to have a look at some of the related articles on my homepage.

 

This post explained how to delete names from a named vector in the R programming language. Don’t hesitate to let me know in the comments section, in case you have additional questions and/or comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top