更新时间:2023-02-11 07:59:18
library(stringr)
x <- str_extract_all(strLine,"\\(?[0-9,.]+\\)?")[[1]]
> x
[1] "0" "3,000" "(500)" "0" "2.25" "(1,200)"
将括号更改为否定:
x <- gsub("\\((.+)\\)","-\\1",x)
x
[1] "0" "3,000" "-500" "0" "2.25" "-1,200"
然后 as.numeric()
或 taRifx::destring
完成(下一版本 destring
将默认支持否定,因此 keep
选项将是必需的):
And then as.numeric()
or taRifx::destring
to finish up (the next version of destring
will support negatives by default so the keep
option won't be necessary):
library(taRifx)
destring( x, keep="0-9.-")
[1] 0 3000 -500 0 2.25 -1200
或:
as.numeric(gsub(",","",x))
[1] 0 3000 -500 0 2.25 -1200