Extract letters from a string in R

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 containing only letters, for example. c("AB", "GF", "ABC") .

Since the number of letters varies, I cannot use substring to indicate the first and last characters.

How can i do this?

+6
source share
4 answers

you can try

 sub("^([[:alpha:]]*).*", "\\1", x) [1] "AB" "GF" "ABC" 
+3
source

None of the answers work if you have a mixed letter with spaces. Here is what I do for these cases:

 x <- c("AB.38.2", "GF.40.4", "ABC.34.2", "AB ..C 312, Fd") unique(na.omit(unlist(strsplit(unlist(x), "[^a-zA-Z]+")))) 

[1] "AB" "GF" "ABC" "A" "B" "C" "Fd"

+2
source

Here is how I managed to solve this problem. I use this because it returns 5 elements, and I can control if I need space between words:

 x <- c("AB.38.2", "GF.40.4", "ABC.34.2", "AB ..C 312, Fd", " a") extract.alpha <- function(x, space = ""){ require(stringr) require(purrr) require(magrittr) y <- strsplit(unlist(x), "[^a-zA-Z]+") z <- y %>% map(~paste(., collapse = space)) %>% simplify() return(z)} extract.alpha(x, space = " ") 
+2
source

I understand that this is an old question, but since I was looking for a similar answer just now and found it, I thought I would share it.

The simplest and quickest solution I found:

 x <- c("AB.38.2", "GF.40.4", "ABC.34.2") only_letters <- function(x) { gsub("^([[:alpha:]]*).*$","\\1",x) } only_letters(x) 

And the result:

 [1] "AB" "GF" "ABC" 

Hope this helps someone!

0
source

Source: https://habr.com/ru/post/989338/


All Articles