Reputation: 1537
If some columns in a data frame share a common prefix, how to I find out such common prefix?
Note: prefix here means the longest substring before any number appears.
data set may look like:
Date,vix1,vix2,vix3,doSG124,doSG220
In this case, I want to get vix
instead of doSG
because more columns (3) have vix
as prefix.
Upvotes: 1
Views: 94
Reputation: 887213
You could try table
and which.max
after removing the 'suffix' part with sub
. Here, I assume that the suffix is the numeric part.
tbl <- table(sub('\\d+$', '', v1))
names(which.max(tbl))
#[1] "vix"
By using sub
, we match the numeric part (\\d+
) to the end of the string ($
) and replace it with ''
v1 <- c('Date', 'vix1', 'vix2', 'vix3', 'doSG124', 'doSG220')
Upvotes: 2