Perform multiple search-and-replaces on the colnames of a dataframe
问题 I have a dataframe with 95 cols and want to batch-rename a lot of them with simple regexes, like the snippet at bottom, there are ~30 such lines. Any other columns which don't match the search regex must be left untouched. **** Example: names(tr) = c('foo', 'bar', 'xxx_14', 'xxx_2001', 'yyy_76', 'baz', 'zzz_22', ...) **** I started out with a wall of 25 gsub()s - crude but effective: names(tr) <- gsub('_1$', '_R', names(tr)) names(tr) <- gsub('_14$', '_I', names(tr)) names(tr) <- gsub('_22$',