3
$\begingroup$

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

$\endgroup$
7
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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"
$\endgroup$
0
$\begingroup$

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]'
$\endgroup$
0
$\begingroup$

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())
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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