Extract all numbers from a single string in R

后端 未结 4 1055
夕颜
夕颜 2020-11-30 07:54

Let\'s imagine you have a string:

strLine <- \"The transactions (on your account) were as follows: 0 3,000 (500) 0 2.25 (1,200)\"

Is the

4条回答
  •  有刺的猬
    2020-11-30 08:10

    Since this came up in another question, this is an uncrutched stringi solution (vs the stringr crutch):

    as.numeric(
      stringi::stri_replace_first_fixed(
        stringi::stri_replace_all_regex(
          unlist(stringi::stri_match_all_regex(
            "The transactions (on your account) were as follows: 0 3,000 (500) 0 2.25 (1,200)", 
            "\\(?[0-9,.]+\\)?"
          )), "\\)$|,", ""
        ),
        "(", "-"
      )
    )
    

提交回复
热议问题