Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subset data based on partial match of column names

Tags:

r

subset

I need to subset a df to include certain strings. Some of these are full column names, and the following works fine:

testData[,c("FullColName1","FullColName2","FullColName3")]

My problem is that I need to expand this to also include column names that contain specific strings that may partially match to some other column names. These strings include letters and symbols:

"PartString1()","PartString2()"

I tried putting wildcards around these. (I've indicated this below with the prefix "star" because the "*" symbol didn't render correctly.)

testData[ ,c("FullColName1","FullColName2","FullColName3",
             "starPartString1()star","starPartString2()star")]

But I'm getting an error message: undefined columns selected. I can't figure out if or how I need grep to make this work.

like image 716
user3614783 Avatar asked Jun 12 '14 04:06

user3614783


People also ask

How do I subset a Dataframe based on column values in R?

How to subset the data frame (DataFrame) by column value and name in R? By using R base df[] notation, or subset() you can easily subset the R Data Frame (data. frame) by column value or by column name.

How do I select a column with certain names in R?

To select a column in R you can use brackets e.g., YourDataFrame['Column'] will take the column named “Column”. Furthermore, we can also use dplyr and the select() function to get columns by name or index. For instance, select(YourDataFrame, c('A', 'B') will take the columns named “A” and “B” from the dataframe.

How do you partially match text in R?

To do a Partial String Matching in R, use the charmatch() function. The charmatch() function accepts three arguments and returns the integer vector of the same length as input.

How do you subset a dataset?

To create a subset data set, specify the name of the subset data set on the DATA statement, bring in the full data set with a SET statement, and specify the subsetting criteria with either subsetting IF statements or WHERE statements.


2 Answers

You mentioned you may be looking for symbols, so for this particular example we can use [[:punct:]] as our regular expression. This will find all the strings with punctuation symbols in the column names.

d <- data.frame(1:3, 3:1, 11:13, 13:11, rep(1, 3))
names(d) <- c("FullColName1", "FullColName2", "FullColName3",
              "PartString1()","PartString2()")

d[grepl("[[:punct:]]", names(d))]
#   PartString1() PartString2()
# 1            13             1
# 2            12             1
# 3            11             1

This last part just illustrates another way to do this with other string processing functions from stringr

library(stringr)
d[str_detect(names(d), "[[:punct:]]")]
#   PartString1() PartString2()
# 1            13             1
# 2            12             1
# 3            11             1

ADD per OPs comment

d[grepl("ring[12()]", names(d))]

to get either of the substrings ring1() or ring2() from the names vector

like image 76
Rich Scriven Avatar answered Oct 06 '22 11:10

Rich Scriven


You can use grep to find indices of column names with partial match to a particular pattern

require(PerformanceAnalytics)
data(managers)

colnames(managers)
#[1] "HAM1"        "HAM2"        "HAM3"        "HAM4"        "HAM5"       
#[6] "HAM6"        "EDHEC LS EQ" "SP500 TR"    "US 10Y TR"   "US 3m TR"

suppose the pattern you want to match is "HAM", along with some fixed column names ("SP500 TR" "US 10Y TR" "US 3m TR")

head(managers[,c("SP500 TR","US 10Y TR","US 3m TR",colnames(managers)[grep("HAM",colnames(managers))])])
#           SP500 TR US 10Y TR US 3m TR    HAM1 HAM2    HAM3    HAM4 HAM5 HAM6
#1996-01-31   0.0340   0.00380  0.00456  0.0074   NA  0.0349  0.0222   NA   NA
#1996-02-29   0.0093  -0.03532  0.00398  0.0193   NA  0.0351  0.0195   NA   NA
#1996-03-31   0.0096  -0.01057  0.00371  0.0155   NA  0.0258 -0.0098   NA   NA
#1996-04-30   0.0147  -0.01739  0.00428 -0.0091   NA  0.0449  0.0236   NA   NA
#1996-05-31   0.0258  -0.00543  0.00443  0.0076   NA  0.0353  0.0028   NA   NA
#1996-06-30   0.0038   0.01507  0.00412 -0.0039   NA -0.0303 -0.0019   NA   NA

you can specify multiple patterns using, grep("pattern1 | pattern2 ", colnames(data))

like image 23
Silence Dogood Avatar answered Oct 06 '22 11:10

Silence Dogood