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 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:
- Get Column Names of Data Frame in R
- Select Variables that Contain Particular String in Column Name
- Change Letter Case of Column Names in R
- R Functions List
- All R Programming Examples
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.






