I have a character vector containing variable names such as x <- c("AB.38.2", "GF.40.4", "ABC.34.2")
. I want to extract the letters so that I have a character vector now containing only the letters e.g. c("AB", "GF", "ABC")
.
Because the number of letters varies, I cannot use substring
to specify the first and last characters.
How can I go about this?
I realize this is an old question but since I was looking for a similar answer just now and found it, I thought I'd share.
The simplest and fastest solution I found myself:
And the output is:
Hope this helps someone!
None of the answers work if you have mixed letter with spaces. Here is what I'm doing for those cases:
The previous answers seem more complicated than necessary. This question regarding digits also works with letters:
you can try
This is how I managed to solve this problem. I use this because it returns the 5 items cleanly and I can control if i want a space in between the words: