Home > OS >  How do you extract just the second decimal value using regexp_extract?
How do you extract just the second decimal value using regexp_extract?

Time:03-16

How do you extract just the second decimal value in the following using regexp_extract? I have the value below in column col1 in dataframe df. Documentation on this is slim and I'm not quite certain on what to use for the expression to describe the pattern (marked with question marks). Desired output is the value 0.9 in a new column called col2 in dataframe df.

{"option1":"option2","option3":4,"options":[0.1,0.9]}

Here is my attempt so far:

df <- df %>%
  mutate(col2 = regexp_extract(col1, "???", 1)

CodePudding user response:

The first gourp in this regex will match '0.9'

^.*\[.*,(.*)?\]\}$

CodePudding user response:

A base R option using sub -

x <- '{"option1":"option2","option3":4,"options":[0.1,0.9]}'

sub('.*\\[\\d \\.\\d ,(\\d \\.\\d )\\].*', '\\1', x)
#[1] "0.9"

.* matches everything until a [, then \\d \\.\\d is the 1st decimal number followed by a comma and we capture the second decimal number.

  • Related