I have a column that basically looks like the following set of strings:
v <- c("1 - IOP A", "1 - PHP B", "1 - RES C", "2 - IOP A", "2 - PHP B", "18 - IOP A", "18 - PHP A", "18 - RES D")
All of these are or will become factors. There are almost 300 of them in my data set. My goal is to combine the levels into categories represented by only the alpha portion of these otherwise alpha-numeric strings. From the example above, the desired output would want would be:
c("IOP A", "PHP B", "RES C", "PHP A", "RES D")
This is what I tried, but it doesn't work, provides no error message, nothing changes, and I'm not sure why or therefore what to do next/what's wrong. I'm assuming it has to do with using the loop index in the string pattern argument, but I don't know how to tell the program to use the index value.
for (i in 1:18) {
sub(pattern = "i - ", replacement = "", v)
}
I would appreciate anyone showing me the most elegant or even multiple ways of accomplishing this as it's a pretty fundamental data engineering task. I'm sure there's an even faster sapply way to write this code, too.
Thanks :)