i have a list of string that looks like this:
list=["chr21-10139833-A-C","chry-10139832-b-f"]
for every string in the list i need to extract the numbers between "-" and "-"
so i would get:
[10139833,10139832]
i tried this :
gsub(".*[-]([^-]+)[-]", "\\1", list
but it returns :
[ac,bf]
what can i do to make it work ? thank you
Using str_extract
from stringr
we can try:
list <- c("chr21-10139833-A-C", "chry-10139832-b-f")
nums <- str_extract(list, "(?<=-)(\\d+)(?=-)")
nums
[1] "10139833" "10139832"
We could also use sub
for a base R option:
list <- c("chr21-10139833-A-C", "chry-10139832-b-f")
nums <- sub(".*-(\\d+).*", "\\1", list)
nums
[1] "10139833" "10139832"
1) Using the input shown in the Note at the end, use read.table
. If you want character output instead add colClasses = "character"
argument to read.table
.
read.table(text = x, sep = "-")[[2]]
## [1] 10139833 10139832
2) Another possibility is to use strapply
. If you want character output then omit the as.numeric
argument.
library(gsubfn)
strapply(x, "-(\\d+)-", as.numeric, simplify = TRUE)
## [1] 10139833 10139832
x <- c("chr21-10139833-A-C", "chry-10139832-b-f")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With