processing negative number in “accounting” format

后端 未结 2 1725
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 22:45

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 inte

2条回答
  •  时光说笑
    2020-11-27 23:21

    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"
    

提交回复
热议问题