r - return most common prefix in columns -
if columns in data frame share common prefix, how find out such common prefix?
note: prefix here means longest substring before number appears.
data set may like:
date,vix1,vix2,vix3,dosg124,dosg220
in case, want vix
instead of dosg
because more columns (3) have vix
prefix.
you try table
, which.max
after removing 'suffix' part sub
. here, assume suffix numeric part.
tbl <- table(sub('\\d+$', '', v1)) names(which.max(tbl)) #[1] "vix"
by using sub
, match numeric part (\\d+
) end of string ($
) , replace ''
data
v1 <- c('date', 'vix1', 'vix2', 'vix3', 'dosg124', 'dosg220')
Comments
Post a Comment