Extract all numbers from a single string in R

后端 未结 4 1144
夕颜
夕颜 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:22

    Here's the base R way, for the sake of completeness...

    x <- unlist(regmatches(strLine, gregexpr('\\(?[0-9,.]+', strLine)))
    x <- as.numeric(gsub('\\(', '-', gsub(',', '', x)))
    [1]     0.00  3000.00  -500.00     0.00     2.25 -1200.00
    

提交回复
热议问题