This is easy to do with regular expressions (the funky pattern that is the first argument to sub
):
names(r1.score) <- sub(".*_(.*)\\..*", "\\1", r1score.list)
The second argument is what we replace the value matched by the regular expression. In this case it is a special symbol \\1
, which is a reference to the part of the pattern matched inside the parentheses, (.*)
. If you look carefully you can see that before the parenthesis we're matching .*_
which means "anything ending in an underscore" and on the other side we're matching \\..*
which means anything starting with a period (we need to use \\.
because otherwise a period is treated as a wild card).