Get the List of column names of dataframe in R

To get the list of column names of dataframe in R we use functions like names() and colnames(). In this section we will be looking on how to get the list of column names in the dataframe with an example

Let’s first create the dataframe.

df1=data.frame(State=c('Arizona','Georgia', 'Newyork','Indiana','seattle','washington','Texas'),
               code=c('AZ','GA','NY','IN','ST','WT','TX'),
               Score=c(62,47,55,74,31,77,85))
df1

So the dataframe will be

Get the List of column names of dataframe in R 1

 

Get the List of column names in R (Method 1):

Get the list of column names using the function colnames()

# method 1

colnames(df1) 

Result:

[1] “State” “state_code” “Score”

 

Get the List of column names in R (Method 2):

Get the list of column names using the function names()

# method 2

names(df1) 

Result:

[1] “State” “state_code” “Score”

 

 

                                                                                                 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.