I have a table in R. It just has two columns and many rows. Each element is a string that contains some characters and some numbers. I need number part of the element. How can I have number part? For example:

1          ce7380       ce6058
2          ce7380      ce13812
3          ce7382       ce7382
4          ce7382       ce5255
5          ce7382       ce1103
6          ce7388        ce523
7          ce7388       ce8534



7 Answers 7


You may use gsub function

> c <-  "ce7382"
> gsub("[a-zA-Z ]", "", c)
[1] "7382"

Feel free to add other characters you need to remove to the regexp and / or to cast the result to number with as.numeric.


If the undesired characters are constant as in the example, like ce7380 where the ce is unwanted, one may try the following:

df <- df %>%
      mutate_at("INTERACTOR_A", str_replace, "ce", "")

This instructs R to perform the mutation function in the column INTERACTOR_A and replace the constant ce with nothing.

If the undesired characters change from row to row, then other regex methods offered here may be more appropriate.


Similar to one of the earlier, you can also apply the logic of extracting everything starting from (i.e. including) the first numeric digit:

interactor <- c("ce7380", "ce7382", "ce7388")
x <- gregexpr("[0-9]+", interactor)
x <- unlist(regmatches(interactor, x))
## [1] "7380" "7382" "7388"

I'd just do it like so:


# either 
this_text <- c('ce7380', 'ce5932', 'ce1234')

# make a new text vector:
new_text <- this_text %-% '[a-z]'

# or make an integer vector:
new_number <- int(this_text %-% '[a-z]')

# OR change this_text in-place
this_text <- c('ce7380', 'ce5932', 'ce1234')

this_text %-=% '[a-z]'

This is an old question but I hope this helps.

md = data.frame(INTERACTOR_A = c("ce7380", "ce7380", "ce7382", "ce7382", "ce7382", 
"ce7388", "ce7388"), 
                INTERACTOR_B = c("ce6058", "ce13812", "ce7382", "ce5255", "ce1103", 
"ce523", "ce8534"))

md_1 = md %>% mutate(INTERACTOR_A = as.character(INTERACTOR_A),
                INTERACTOR_B = as.character(INTERACTOR_B)) %>% 
            mutate(INTERACTOR_A = substr(INTERACTOR_A, 3, nchar(INTERACTOR_A)) %>% 
                 INTERACTOR_B = substr(INTERACTOR_B, 3, nchar(INTERACTOR_B)) %>% 

Try this if you are not sure about characters in string:

{as.numeric(gsub(",","", x)}

Although this is an old question, I am still hoping that my answer helps.

There are two ways you can deal with this problem:

df = data.frame(INTERACTOR_A = c("ce7380","ce7380",'ce7382','ce7382','ce7382','ce7388','ce7388'),
            INTERACTOR_B = c("ce6058","ce13812","ce7382","ce5255","ce1103","ce523","ce8534"))

First you could use:

num_data <- as.numeric(str_extract(df$INTERACTOR_A, "[0-9]+"))

     # Output – 7380 7380 7382 7382 7382 7388 7388

Second method using the gsub() function:

num_data <- gsub('\\D','', df$INTERACTOR_B) 

     # Output – '6058' '13812' '7382' '5255' '1103' '523' '8534'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.