How to check if column exists in data frame in R

In R, you can use the colnames() function to check if a column exists in a data frame. This function will return a character vector containing the names of all the columns in the data frame. If the column you are looking for is included in the list, then it exists in the data frame. Alternatively, you can also use the colnames() function with the which() function to return the column index of the column you are looking for. If the which() function returns TRUE, then the column exists in the data frame.


You can use the following methods to check if a column exists in a data frame in R:

Method 1: Check if Exact Column Name Exists in Data Frame

'this_column' %in% names(df)

Method 2: Check if Partial Column Name Exists in Data Frame

any(grepl('partial_name', names(df)))

Method 3: Check if Several Exact Column Names All Exist in Data Frame

all(c('this_column', 'that_column', 'another_column') %in% names(df))

This tutorial explains how to use each method in practice with the following data frame:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    B     90      28       28
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28

Example 1: Check if Exact Column Name Exists in Data Frame

The following code shows how to check if the exact column name ‘rebounds’ exists in the data frame:

#check if exact column name 'rebounds' exists in data frame
'rebounds' %in% names(df)

[1] TRUE

The output returns TRUE.

This tells us that the exact column name ‘rebounds’ does exist in the data frame.

Note: This syntax is case-sensitive. This means if we used ‘Rebounds’ then we would receive a value of FALSE since the name ‘Rebounds’ with a capital letter does not exist in the data frame.

Example 2: Check if Partial Column Name Exists in Data Frame

The following code shows how to check if the partial column name ‘tea’ exists in the data frame:

#check if partial column name 'tea' exists in data frame
any(grepl('tea', names(df)))

[1] TRUE

The output returns TRUE.

This tells us that the partial column name ‘tea’ does exist in the data frame.

Example 3: Check if Several Exact Column Names All Exist in Data Frame

The following code shows how to check if the names ‘team’, ‘points’, and ‘blocks’ all exist in the data frame:

#check if three column names all exist in data frame
all(c('team', 'points', 'blocks') %in% names(df))

[1] FALSE

The output returns FALSE.

This tells us that all three column names we checked do not all exist in the data frame.

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

x