How to Check if Column Exists in Data Frame in R


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.

Additional Resources

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

How to Select Columns Containing a Specific String in R
How to Remove Characters from String in R
How to Find Location of Character in a String in R

Leave a Reply

Your email address will not be published.