How to get column names in R?

In R, you can obtain the column names of a data frame or matrix by using the colnames() function, which returns a character vector containing the column names of the object passed in as an argument. You can also set the column names of a data frame or matrix by passing in a character vector as an argument to the colnames() function. Additionally, you can use the names() function to get and set the row names of an object.


You can use the following methods to get the column names of a data frame in R:

Method 1: Get All Column Names

colnames(df)

Method 2: Get Column Names in Alphabetical Order

sort(colnames(df))

Method 3: Get Column Names with Specific Data Type

colnames(df[,sapply(df,is.numeric)])

The following examples show how to use each method with the following data frame in R:

#create data frame
df = data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F'),
                points=c(18, 22, 19, 14, 14, 11),
                assists=c(5, 7, 7, 9, 12, 9),
                playoffs=c(TRUE, FALSE, FALSE, TRUE, TRUE, TRUE))

#view data frame
df

  team points assists playoffs
1    A     18       5     TRUE
2    B     22       7    FALSE
3    C     19       7    FALSE
4    D     14       9     TRUE
5    E     14      12     TRUE
6    F     11       9     TRUE

Example 1: Get All Column Names

The easiest way to get all of the column names in a data frame in R is to use colnames() as follows:

#get all column names
colnames(df)

[1] "team"     "points"   "assists"  "playoffs"

The result is a vector that contains all four column names from the data frame.

Example 2: Get Column Names in Alphabetical Order

To get the column names in a data frame in alphabetical order, you can use the sort() function as follows:

#get column names in alphabetical order
sort(colnames(df))

[1] "assists"  "playoffs" "points"   "team"   

The result is a vector that contains all four column names from the data frame listed in alphabetical order.

#get column names in reverse alphabetical order
sort(colnames(df), decreasing=TRUE)

[1] "team"     "points"   "playoffs" "assists" 

Example 3: Get Column Names with Specific Data Type

You can use the following syntax to view the data type of each column in the DataFrame:

#view data type of each column
str(df)

'data.frame':	6 obs. of  4 variables:
 $ team    : chr  "A" "B" "C" "D" ...
 $ points  : num  18 22 19 14 14 11
 $ assists : num  5 7 7 9 12 9
 $ playoffs: logi  TRUE FALSE FALSE TRUE TRUE TRUEt

You can then use the sapply() function to only get the column names with a specific data type.

For example, we can use the following syntax to only get the column names that have a data type of numeric:

#get all columns that have data type of numeric
colnames(df[,sapply(df,is.numeric)])

[1] "points"  "assists"

The result is a vector of column names that have a data type of numeric.

The following tutorials explain how to perform other common operations in R:

x