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 |
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" |
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" |
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 |
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" |
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 |
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 |
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:
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.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may have a look at the other articles of https://www.statisticsglobe.com/.
- Convert Named Vector to Data Frame
- Convert Data Frame with Date Column to Time Series Object
- Convert Vector to List in R
- Convert UNIX Timestamp to Date Object
- Convert Character String to Date Object
- R Functions List (+ Examples)
- The R Programming Language
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.
Statistics Globe Newsletter
2 Comments. Leave new
Hi Joachim, I found your tutorials quite helpful. Sometimes I have been missing a bit more context though. In this case, I am wondering about the difference between the name and a character string. I guess I shall find the answer somewhere. In any case, thank you for your hard work.
Hello Jan,
Thank you for the feedback. It is great to hear that we can help. Regarding your question, you can check the R documentation for the name and symbol class; see https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/name. I have also written a short code to explain the class’s functionality.
Regards,
Cansu