4.2 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Check if Column Exists in Data Frame in R

How to Check if Column Exists in Data Frame in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories