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 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 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.
- Insert Character Pattern at Particular Position of String
- Print Character String & Variable on Same Output Line
- Split Data Frame Variable into Multiple Columns
- Convert Character String to Variable Name in R
- R Programming Overview
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.