1

read_csv's col_number doesn't handle bracket-style negatives. This solution works, but I'm curious to know if there is a simpler one?

library(tidyverse)
library(stringr)
data <- read_csv2("num\n12\n(12)\n13\n£2,250.00")
data$num <- data$num %>% 
  str_replace("^\\(([1-9]+)\\)$","-\\1") %>% 
  parse_number()
head(data)

# A tibble: 4 x 1
    num
  <dbl>
1    12
2   -12
3    13
4  2250
Carl
  • 163
  • 1
  • 10
  • 1
    It doesn't look like there's an easy (out of the box) way to handle this. One could also create a new data type that interprets parentheses (or brackets) as a negative number: https://stackoverflow.com/questions/5068705/processing-negative-number-in-accounting-format – jdobres Sep 05 '17 at 17:03

0 Answers0