Remove part of string in R

3

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:

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

Thanks

mahtab

Posted 2016-12-07T13:10:52.220

Reputation: 31

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.

Marmite Bomber

Posted 2016-12-07T13:10:52.220

Reputation: 1 073

0

Try this if you are not sure about characters in string {as.numeric(gsub(",","", x)}

Rahul Sharma

Posted 2016-12-07T13:10:52.220

Reputation: 174

0

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))
x
## [1] "7380" "7382" "7388"

David C.

Posted 2016-12-07T13:10:52.220

Reputation: 105

0

I'd just do it like so:

library(roperators)

# 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]'

Benbob

Posted 2016-12-07T13:10:52.220

Reputation: 1

0

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)) %>% 
as.numeric(),
                 INTERACTOR_B = substr(INTERACTOR_B, 3, nchar(INTERACTOR_B)) %>% 
as.numeric())

sammgolo

Posted 2016-12-07T13:10:52.220

Reputation: 1