我有一个格式为 Excel 中“会计”样式的数据,看起来像($317.40)
或$13,645.48
. 作为一个正则表达式新手,我正在寻找一种更有效的方法来删除所有无用的符号并将带括号的字符串转换为负数。
到目前为止,我是这样做的:
spending$Amount <- gsub("^[(]", "-", spending$Amount)
spending$Amount <- gsub("[$]", "", spending$Amount)
spending$Amount <- gsub("[)]", "", spending$Amount)
spending$Amount <- as.numeric(gsub("[,]", "", spending$Amount))
我可以在一行中做到这一点吗?是否有专门的 R 包可以做到这一点?