How to Check if String Contains Specific Characters in R


Often you may want to know if a string contains specific characters in R.

The easiest way to do so is by using the grepl() function, which was built for this exact task.

You can use the following methods with the grepl() function to check if a string contains specific characters:

Method 1: Check if String Contains Specific Characters (case-sensitive)

#check if each string in 'team' column contains 'avs' (case-sensitive)
grepl('avs', df$team, fixed=TRUE)

Note that fixed=TRUE specifies that the pattern in the first argument should not be treated as regex.

Method 2: Check if String Contains Specific Characters (case-insensitive)

#check if each string in 'team' column contains 'avs' (case-insensitive)
grepl('AVS', toupper(df$team), fixed=TRUE)

Note that toupper() converts each string in the team column to all uppercase before checking if the pattern ‘AVS’ exists in each string.

Method 3: Check if String Contains One of Several Specific Characters

#check if each string in 'team' column contains either a v or a k
grepl('v|k', df$team)

Note that the | operator represents “OR” logic and allows us to check if the character “v” or the character “k” exists in each sting in the team column.

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(team=c('Mavs', 'Hawks', 'Cavs', 'Magic', 'Heat', 'Nets'),
                 points=c(22, 25, 18, 13, 40, 23),
                 assists=c(8, 12, 10, 15, 13, 7))

#view data frame
df

   team points assists
1  Mavs     22       8
2 Hawks     25      12
3  Cavs     18      10
4 Magic     13      15
5  Heat     40      13
6  Nets     23       7

Example 1: Check if String Contains Specific Characters (case-sensitive)

We can use the following syntax to check if each string in the team column contains the pattern “avs” anywhere in the string:

#check if each string in 'team' column contains 'avs' (case-sensitive)
df$avs <- grepl('avs', df$team, fixed=TRUE)

#view updated data frame
df

   team points assists   avs
1  Mavs     22       8  TRUE
2 Hawks     25      12 FALSE
3  Cavs     18      10  TRUE
4 Magic     13      15 FALSE
5  Heat     40      13 FALSE
6  Nets     23       7 FALSE

The new avs column returns the following values:

  • TRUE if team contains “avs”
  • FALSE if team does not contain “avs”

Example 2: Check if String Contains Specific Characters (case-insensitive)

We can use the following syntax to check if each string in the team column contains the pattern “avs” anywhere in the string:

#check if each string in 'team' column contains 'avs' (case-insensitive)
df$avs <- grepl('AVS', toupper(df$team), fixed=TRUE)

#view updated data frame
df

   team points assists   avs
1  Mavs     22       8  TRUE
2 Hawks     25      12 FALSE
3  Cavs     18      10  TRUE
4 Magic     13      15 FALSE
5  Heat     40      13 FALSE
6  Nets     23       7 FALSE

The new avs column returns the following values:

  • TRUE if team contains “avs” (case-insensitive)
  • FALSE if team does not contain “avs” (case-insensitive)

Example 3: Check if String Contains One of Several Specific Characters

We can use the following syntax to check if each string in the team column contains the pattern “avs” anywhere in the string:

#check if each string in 'team' column contains 'v' or 'k'
df$avs <- grepl('v|k', df$team)

#view updated data frame
df

   team points assists   avs
1  Mavs     22       8  TRUE
2 Hawks     25      12  TRUE
3  Cavs     18      10  TRUE
4 Magic     13      15 FALSE
5  Heat     40      13 FALSE
6  Nets     23       7 FALSE

The new avs column returns the following values:

  • TRUE if team contains either “v” or “k”
  • FALSE if team does not contain either “v” or “k”

Additional Resources

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

R: How to Check if Character is in String
R: How to Remove Spaces from Strings
R: How to Extract String Between Specific Characters

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *