Get Value of Data Element without Name or Index in R (3 Examples)

 

This tutorial explains how to return only the value of a data table element without showing the name or index in the R programming language.

The tutorial contains these topics:

Let’s jump right to the examples!

 

Creating Example Data

First, let’s construct some example data in R:

set.seed(872634)          # Create example table
data <- table(sample(letters[1:5], 100, replace = TRUE))
data                      # Print example table
# 
#  a  b  c  d  e 
# 14 22 20 24 20

The previous output of the RStudio console shows the structure of our example data: A data table with five columns and one row.

Let’s extract only one of the table elements using single square brackets:

data[3]                   # Return element of table with name
#  c 
# 20

As you can see, the previous R code returned the third value and the name of this table column.

Next, I’ll explain how to print only the value without any names or indices.

Let’s do this!

 

Example 1: Extract Data Element without Name Using Double Square Brackets

In Example 1, I’ll explain how to use double square brackets to get rid of all names and indices of a data value.

Have a look at the following R code:

data[[3]]                 # Return element of table without name
# [1] 20

As you can see, the RStudio console have returned only the value without name or index.

 

Example 2: Extract Data Element without Name Using as.vector Function

As an alternative to Example 1, Example 2 shows how to use the as.vector function to return a data element without index or name:

as.vector(data[3])        # Return element of table without name
# [1] 20

 

Example 3: Extract Data Element without Name Using as.double Function

Another alternative is provided by the as.double function:

as.double(data[3])        # Return element of table without name
# [1] 20

 

Video & Further Resources

Have a look at the following video of my YouTube channel. In the video, I’m explaining the content of this article in a live session in R.

 

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.

 

In addition, you might want to have a look at some of the other tutorials on this homepage:

 

Summary: In this article you have learned how to extract only values without names or indices in the R programming language. In case you have additional comments and/or questions, let me know in the 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.

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

Menu
Top