hasName Function in R (Example)


In this R tutorial you’ll learn how to apply the hasName function.

The article will consist of these topics:

Let’s dive into it!


Creation of Example Data

Have a look at the following example data.

data <- data.frame(x1 = 1:5,    # Create example data
                   x2 = 5:1)
data                            # Print example data


table 1 data frame hasname function


Table 1 shows that our example data consists of five rows and two columns.


Example: How to Apply the hasName Function

In this section, I’ll illustrate how to use the hasName function to check whether a data frame object contains a certain variable name.

Consider the following R code:

hasName(data, "x1")             # Apply hasName function
# [1] TRUE

The previous syntax has checked whether our example data frame contains a column with the name “x1”. As you can see, the RStudio console has returned the logical indicator TRUE, i.e. our data frame contains a column with the name “x1”.

Let’s test another variable name:

hasName(data, "x3")             # Apply hasName function
# [1] FALSE

The column “x3” does not exist in our data frame.


Video, Further Resources & Summary

In case you need further information on the R syntax of this article, you might have a look at the following video on my YouTube channel. In the video, I show the R programming code of this tutorial.



Furthermore, you could read the other tutorials on this website:


In summary: This article has illustrated how to use the hasName command in R. In case you have any further questions, let me know in the comments section.


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.