Home > Mobile >  Using R-substr for assignment
Using R-substr for assignment

Time:10-29

I have the element: MVa.8.199038.R I only want to extract the number 199038, using R. However, I want this to be more general. So, I need a code that will read this element from right to left (starting from the dot) and end to the next dot. This element is part of a data frame.

I tried this: substr((df$marker), nchar(df$marker) - 2, \.), but this does not give output.

CodePudding user response:

Possibly with a look ahead using stringer. Without more cases from your data it's hard to be sure how general this might be. I've assumed your number can comprise between 1 and 10 digits. To convert to a number wrap the expression in as.numeric.

x <- "MVa.8.199038.R"


stringr::str_extract(x, "\\d{1,10}(?=\\.[^0-9].*$)")
#> [1] "199038"

Created on 2022-10-28 with reprex v2.0.2

CodePudding user response:

A couple other options:

x <- "MVa.8.199038.R"

#option 1
sub("^.*?(\\d )\\.R$", "\\1", x)
#> [1] "199038"

#option 2
stringr::str_extract(x, "\\d (?=\\.R)")
#> [1] "199038"
  •  Tags:  
  • r
  • Related