R: How to Check if Column Contains String


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

Method 1: Check if Exact String Exists in Column

sum(str_detect(df$column_name, '^exact_string$')) > 0

Method 2: Check if Partial String Exists in Column

sum(str_detect(df$column_name, 'partial_string')) > 0

Method 3: Count Occurrences of Partial String in Column

sum(str_detect(df$column_name, 'partial_string'))

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

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'C'),
                 conf=c('East', 'East', 'South', 'West', 'West', 'East'),
                 points=c(11, 14, 15, 15, 14, 19))

#view data frame
df

  team  conf points
1    A  East     11
2    A  East     14
3    A South     15
4    B  West     15
5    B  West     14
6    C  East     19

Example 1: Check if Exact String Exists in Column

The following code shows how to check if the exact string ‘Eas’ exists in the conf column of the data frame:

#check if exact string 'Eas' exists in conf column
sum(str_detect(df$conf, '^Eas$')) > 0

[1] FALSE

The output returns FALSE.

This tells us that the exact string ‘Eas’ does not exist in the conf column.

Note: We used regex symbols to indicate the start ( ^ ) and end ( $ ) characters of the string we were looking for.

Example 2: Check if Partial String Exists in Column

The following code shows how to check if the partial string ‘Eas’ exists in the conf column of the data frame:

#check if partial string 'Eas' exists in conf column
sum(str_detect(df$conf, 'Eas')) > 0

[1] TRUE

The output returns TRUE.

This tells us that the partial string ‘Eas’ does exist in the conf column of the data frame.

Example 3: Count Occurrences of Partial String in Column

The following code shows how to count the number of times the partial string ‘Eas’ occurs in the conf column of the data frame:

#count occurrences of partial string 'Eas' in conf column
sum(str_detect(df$conf, 'Eas'))

[1] 3

The output returns 3.

This tells us that the partial string ‘Eas’ occurs 3 times in the conf column of the data frame.

Related: How to Use str_detect() Function in R

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. Required fields are marked *