Does this column contain the specified string?

Does this column contain the specified string?

This statement refers to a specific column in a dataset and questions whether or not it includes a particular string of characters. It is a formal inquiry aimed at determining the presence of a specific data point within a designated column.

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

Cite this article

stats writer (2024). Does this column contain the specified string?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/does-this-column-contain-the-specified-string/

stats writer. "Does this column contain the specified string?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/does-this-column-contain-the-specified-string/.

stats writer. "Does this column contain the specified string?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/does-this-column-contain-the-specified-string/.

stats writer (2024) 'Does this column contain the specified string?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/does-this-column-contain-the-specified-string/.

[1] stats writer, "Does this column contain the specified string?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. Does this column contain the specified string?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top