as.name & is.name Functions in R (2 Examples)

 

This tutorial explains how to apply the as.name and is.name functions in the R programming language.

The article will consist of two examples for the application of the as.name and is.name commands. More precisely, the tutorial contains the following content:

Let’s get started…

Definitions & Basic R Syntaxes of as.name and is.name Functions

 

Definitions: You can find the definitions of the as.name and is.name functions below.

  • The as.name R function converts a character string to a name class object.
  • The is.name R function tests whether a data object has the class name.

 

Basic R Syntaxes: Please find the basic R programming syntaxes of the as.name and is.name functions below.

as.name(x)                  # Basic R syntax of as.name function
is.name(x)                  # Basic R syntax of is.name function

 

Note that the as.name and is.name functions are synonyms to the as.symbol and is.symbol functions. You can exchange these function names if you want.

However, I’ll show in the following two examples how to use the as.name and is.name commands and functions in the R programming language.

 

Example Data

The following data will be used as basement for this R programming tutorial:

x <- "my_name"              # Create character string object
x                           # Print character string object
# "my_name"

Have a look at the previous output of the RStudio console. It shows that our example data is a simple character string stored in the data object x.

We can use the class function to check the data type of our data object:

class(x)                    # Check class
# "character"

Our example data has the class character.

 

Example 1: Convert Character String to name Class

In Example 1, I’ll show how to change the character class to the name class using the as.name function. Have a look at the following R code:

x_name <- as.name(x)        # Apply as.name function

The previous R syntax switched our data type from character to name. We can check the data type of our new data object using the class function:

class(x_name)               # Check class
# "name"

The new data object has the name data mode.

 

Example 2: Check if Data Object has Class name

We can use the is.name function to return a logical value indicating whether a data object has the class name. Let’s apply the is.name function to our original data object x:

is.name(x)                  # Test character string
# FALSE

As the RStudio console output shows: Our original data object is not a name object.

Now, let’s apply the is.name function to our new data object:

is.name(x_name)             # Test name object
# TRUE

The is.name function returns the logical value TRUE, i.e. our new data object has the class name.

 

Video & Further Resources

Do you need further info on the R programming syntax of this article? Then I can recommend to watch the following video of my YouTube channel. In the video, I’m explaining the R programming codes of this tutorial in a live session in R:

 

The YouTube video will be added soon.

 

Furthermore, you may have a look at the other articles of https://www.statisticsglobe.com/.

 

You learned in this article how to use the as.name and is.name functions in the R programming language. Don’t hesitate to let me know in the comments, if you have additional questions.

 

Subscribe to my free statistics 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.

Menu
Top