Skip to content
Related Articles

Related Articles

hasName() Function in R

Improve Article
Save Article
  • Last Updated : 02 Jun, 2022
Improve Article
Save Article

In this article, we are going to discuss hasName() function in R Programming Language.

hasName() Function

hasName() is used to check whether the dataframe object has a certain name or not.

Syntax: hasName(dataframe,”name”)

where,

dataframe is the input dataframe and name is the variable present as variable(column_name) in the given dataframe. It will return TRUE, if the name exists otherwise it will return FALSE.

Example 1:

In this example, we are creating a dataframe with 4 rows and 3 columns.  Here column names are col1, col2 and col3.

R




# create dataframe
data=data.frame(col1=c(12,2,3,4),
                col2=c(34,32,1,0),
                col3=c(2,45,3,2))
  
# display
print(data)
  
# check for name col1 
print(hasName(data,"col1"))
  
# check for name col2
print(hasName(data,"col2"))
  
# check for name col3
print(hasName(data,"col3"))


Output:

  col1 col2 col3
1   12   34    2
2    2   32   45
3    3    1    3
4    4    0    2
[1] TRUE
[1] TRUE
[1] TRUE

Example 2:

In this example, we are creating a dataframe with 4 rows and 3 columns.  Here column names are col1, col2 and col3.

R




# create dataframe
data = data.frame(col1=c(12,2,3,4),
                  col2=c(34,32,1,0),
                  col3=c(2,45,3,2))
  
# display
print(data)
  
# check for name col4 
print(hasName(data,"col4"))
  
# check for name col6
print(hasName(data,"col6"))


Output:

  col1 col2 col3
1   12   34    2
2    2   32   45
3    3    1    3
4    4    0    2
[1] FALSE
[1] FALSE

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!