Select Variables that Contain Particular String in Column Name in R (Example)

 

In this R tutorial you’ll learn how to extract variables with a partial string match in the column name.

The content of the page looks as follows:

You’re here for the answer, so let’s get straight to the example:

 

Creation of Example Data

At the start, we’ll have to create some data that we can use in the example code later on:

data <- data.frame(col1 = 1:5,                      # Create example data
                   x1 = letters[1:5],
                   col2 = letters[5:1],
                   x2 = 5:1)
data                                                # Print example data

 

table 1 data frame select variable that contain particular string r

 

Table 1 shows that our exemplifying data is composed of five rows and the four columns “col1”, “x1”, “col2”, and “x2”.

 

Example: Select Column Names with Partial String Match Using grep() Function

The following code explains how to extract all columns of a data frame with a partial character string match in their column name.

For this task, we can use the grep function as shown in the following R code:

data_match <- data[ , grep("col", colnames(data))]  # Find matches
data_match                                          # Print subset of data

 

table 2 data frame select variable that contain particular string r

 

Table 2 shows the output of the previous R programming syntax: A data frame subset containing only those variables that have a partial string match with the character string “col”.

 

Video & Further Resources

Have a look at the following video on my YouTube channel. I show the R programming codes of this article in the video:

 

 

Furthermore, you could read the other articles on my homepage.

 

Summary: In this tutorial you have learned how to select all variables with a partial string match in the column name in R programming. Note that the same logic could be applied when we want to remove or drop variables from a data frame. If you have any further questions, please let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top