Convert character string to name class object
Convert character string to name class object, The R programming language’s as.name and is.name functions are demonstrated in this tutorial.
Let’s get going…
Definitions: The as.name and is.name functions are described in the sections that follow.
A character string is transformed into a name class object using the as.name R function.
The R function is.name determines whether a data object contains the class name.
Please see the fundamental R programming syntaxes for the functions as.name and is.name below.
as.name(x) # as.name function basic R syntax
is.name(x) # Basic R syntax of is.name function
It should be noted that the functions as.name and is.name are equivalent to as.symbol and is.symbol. If you’d like, you can swap out these function names.
But, we’ll demonstrate how to use the R programming language’s as.name and is.name commands and functions in the following two examples.
The following information will be used as the starting point for this R programming tutorial:
x <- "myname" # Create character string object x # Print character string object
Have a peek at the RStudio console’s earlier output. It demonstrates that the sample data we used was just a plain character string kept in the data object x.
To determine the data type of our data object, we can utilize the class function.
class(x) # Check class "character"
The class character appears in the example data.
Example 1: Convert character string to name class object
We’ll demonstrate how to use the as.name function to convert the character class to the name class in Example 1. Consider the following R code.
x_name <- as.name(x) # Apply as.name function
Our data type was altered from character to name in the prior R syntax. With the class function, we can determine the data type of our new data object:
class(x_name) # Check class "name"
New class name is “name” mode
Example 2: Verify the class name of the data object.
In order to determine whether a data object contains the class name, we can use the is.name function, which returns a logical value.
Let’s use our initial data object x to test the is.name function.
is.name(x) # Test character string FALSE
As evidenced by the output from the RStudio console, our original data object is not a name object.
Let’s use our new data object to apply the is.name function now.
is.name(x_name) # Test name object TRUE
The is.name function returns the logical value YES, indicating that the class name is present in our new data object.