在“会计"处理中处理负数.格式 [英] processing negative number in "accounting" format

查看:131
本文介绍了在“会计"处理中处理负数.格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,它的负值在数字旁边用括号括起来,即(10)==-10,它是csv格式,如何处理它,以便R将(10)解释为-10?谢谢.

I have dataset, which negative value is presented with a bracket around the number i.e. (10)==-10, it is in csv format, how can I process it so that R will interpret the (10) as -10? Thank you.

更新 我知道我可以通过将(替换为-,删除)并在以后使用as.numeric来解决此问题,但是有没有更优雅的方式解决此问题?

UPDATE I know I can work it out by replacing ( as -, remove ), and use as.numeric afterwards, but is there a more elegant way for this issue?

推荐答案

如果您为记帐格式创建"as.acntngFmt"方法,则可以读取(或使用colClasses("acnt通过文本连接重新读取) ).

If you create an "as.acntngFmt" method for the accounting format, you can read (or perhaps re-read with a text connection using colClasses("acnt").

 setClass("acntngFmt")
 # [1] "acntngFmt"
 setAs("character", "acntngFmt",
    function(from) as.numeric( gsub("\\)", "", gsub("\\(", "-", from))))

  Input <- "A, B, C
  (1.76), 1%, 3.50€
  2.00, 2%, 4.77€
  3.000, 3% , €5.68"

   DF <- read.csv(textConnection(Input), header = TRUE,
     colClasses = c("acntngFmt", "character", "character"))
   str(DF)
'data.frame':   3 obs. of  3 variables:
 $ A: num  -1.76 2 3
 $ B: chr  "1%" "2%" "3%"
 $ C: chr  "3.50€" "4.77€" "€5.68"

这篇关于在“会计"处理中处理负数.格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆