Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

col_number() drops negatives #308

Closed
gregmacfarlane opened this issue Nov 3, 2015 · 7 comments
Closed

col_number() drops negatives #308

gregmacfarlane opened this issue Nov 3, 2015 · 7 comments
Assignees

Comments

@gregmacfarlane
Copy link

@gregmacfarlane gregmacfarlane commented Nov 3, 2015

If I don't request specific column types, then read_csv treats negative numbers appropriately.

readr::read_csv("A,B\n-4,5G")

##    A B
## 1 -4 5

If I specify that the column is numeric, then read_csv drops the negative signs.

readr::read_csv("A,B\n-4,5G", col_types = "nn")

##   A B
## 1 4 5
gregmacfarlane added a commit to gregmacfarlane/trucksim_disagg that referenced this issue Nov 3, 2015
Also fixed reading the coordinates in, see tidyverse/readr#308
@dyndna
Copy link

@dyndna dyndna commented Nov 9, 2015

Specifying col_types= "d" will fix it, although I thought that n should be numeric and should not convert negative to positive values.

@hadley: Thanks for detailed documentation which I did not read until I noticed col_types issue in my data set. Following helped me to understand col_types.

?read_tsv and vignette("column-types")

@rkarimi
Copy link

@rkarimi rkarimi commented Nov 25, 2015

Had the same problem, and was not sure what is the difference between n and d!

@ctmann
Copy link

@ctmann ctmann commented Mar 30, 2016

This introduces a new problem; d converts a number with commas to NA, but at least it warns the user. To confirm, please try the code below using the data on the attached spreadsheet, importing it using three different techniques (base R, readr without explicitly decaring coltypes, and finally, using your solution)

unzip_folder <- "~/R/TestBed/"
file <- "testbed.csv"
filename <- paste0(unzip_folder,file)


read.csv(filename)


library(readr)
read_csv(filename)


read_csv(filename, col_types = cols(
  Name = "c",
  Amounts = "d"))

testbed.zip

@hadley
Copy link
Member

@hadley hadley commented Jun 2, 2016

This issue actually illustrates two problems:

  1. parse_number("-1) returns 1
  2. collector_guess("1G") returns "number"

@hadley hadley changed the title Explicit column types ignore negatives col_number() drops negatives Jun 2, 2016
@jimhester jimhester self-assigned this Jun 7, 2016
jimhester added a commit to jimhester/readr that referenced this issue Jun 8, 2016
@jtr13
Copy link

@jtr13 jtr13 commented Jun 26, 2016

col_number / parse_number doesn't work with negative currencies. (I'm using the dev version.) Thanks.

> parse_number("$3")
[1] 3
> parse_number("-$3")
Warning: 1 parsing failure.
row col expected actual
  1  -- a number     -$
[1] NA
attr(,"problems")
Source: local data frame [1 x 4]

    row   col expected actual
  <int> <int>    <chr>  <chr>
1     1    NA a number     -$
> 

@ctmann
Copy link

@ctmann ctmann commented Dec 10, 2016

This fails:
parse_number("-$3,000")

..but this works:
parse_number("$-3,000")

@lock
Copy link

@lock lock bot commented Sep 25, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants