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.