processing negative number in “accounting” format

后端 未结 2 1722
隐瞒了意图╮
隐瞒了意图╮ 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:23

    If you know the surrounding parentheses will be the only ones in the unit, you can create a function to deal with them:

    test <- c(10, "(10)", 5)
    negative_paren <- function(vec){
      #the backspace escapes the special "(" character
      vec <- gsub("\\(","-",vec) 
      vec <- gsub("\\)","",vec)
      vec <- as.numeric(vec)
      return(vec)
    }
    negative_paren(test)
    [1]  10 -10   5
    

提交回复
热议问题